链接列表导致异常,但不确定原因
LinkedList causes exception but not sure why?
这段代码类似于这里已经有的几篇帖子,但是我遇到了一个独特的问题。链表在尝试插入对象时导致异常。应该有 5 名玩家,每个人都有不同的武器数量,每种武器都有不同的回合数。我不知所措。请帮忙!
LinkedList.h
template <class T>
class LinkedList {
public:
T *first;
LinkedList();
~LinkedList();
inline bool IsEmpty();
inline void Insert(T *);
void Display()const;
T *Find(const int key)const;
T *Delete();
T *Delete(const int key);
};
template <class T>
LinkedList<T>::LinkedList(){
first = 0;
}
template <class T>
LinkedList<T>::~LinkedList(){
}
template <class T>
bool LinkedList<T>::IsEmpty(){
return (first == 0);
}
template <class T>
void LinkedList<T>::Insert(T * newLink){
newLink->next = first; //exception break highlights here
first = newLink;
}
template <class T>
void LinkedList<T>::Display()const {
T *current = first;
while (current != 0) {
current->Display();
current = current->next;
}
}
template <class T>
T *LinkedList<T>::Find(const int key)const {
T *current = first;
while (current->data != key){
if (current->next == 0)
return 0;
else
current = current->next;
}
return current;
}
template <class T>
T *LinkedList<T>::Delete() {
T *temp = first;
first = first->next;
return temp;
}
玩家.h
class Player:public GameObject
{
public:
Player* leftChild;
Player* rightChild;
LinkedList<Weapon>* weapons;
Player();
~Player();
void Display();
bool operator != (const Player&);
bool operator <(const Player&);
};
播放器.cpp
Player::Player()
{
leftChild = 0;
rightChild = 0;
}
Player::~Player()
{
}
void Player::Display()
{
}
bool Player::operator<(const Player& player)
{
if (this->instances < player.instances)
{
return true;
}
else
{
return false;
}
}
bool Player::operator!=(const Player& player)
{
if (instances == NULL)
{
return false;
}
else
{
return true;
}
}
主.cpp
int main()
{
Player *players[4];
players[0] = new Player();
players[1] = new Player();
players[2] = new Player();
players[3] = new Player();
players[4] = new Player();
players[0]->weapons->Insert(new Weapon(1));
}
希望我已经包含了所有内容。我不知道我做错了什么,也不知道该何去何从。
您没有在玩家的构造函数中创建武器对象.cpp:
Player::Player()
{
leftChild = 0;
rightChild = 0;
weapons = new LinkedList<Weapon>;
}
您还可以使武器收集非指针字段对您来说更容易管理。还可以考虑使用 STL 集合而不是您自己的集合(减少出现令人讨厌的异常的风险)...
编辑:是的,黄先生是对的。尝试使用list<Weapon>
而不是实现不错的push_front
和push_back
方法的LinkedList<Weapon>
,也尽量避免使用指针,除非您真正看到它们的使用目的......
您
还需要分配weapons
。例如在 Player() 构造函数中:
Player::Player() {
leftChild = 0;
rightChild = 0;
weapons = new LinkedList<Weapon>;
}
否则,players[0]->weapons
指向无效指针,从而导致异常。
相关文章:
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 不确定如何装饰我的C++库代码以在 C 中使用
- C++功能泄漏内存,我是C++新手,不确定如何解决
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 为什么以下代码是不确定的?
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- 误用指针会产生分段错误。我仍然不确定如何纠正它
- 我不确定为什么代码没有运行,它在编译时抛出错误,说它无法找到 PrintArray()
- 不确定出了什么问题(字符串输入)
- C++睡眠时间不确定
- 在C++中做一个场景问题,我不确定如何处理布尔和if循环
- 代码输出随机符号,我不确定出了什么问题
- 可变值变化,不确定原因
- 有没有办法让C++从 cin 中接收不确定数量的字符串?
- 尝试一次将单个字符读取到大小不确定的数组中
- 我不确定如何引用此神经网络训练方法中的权重
- 升级到G++4.8-exception_ptr.h不支持异常传播
- 存在内存访问异常,但我不确定我的代码中出了什么问题
- 链接列表导致异常,但不确定原因