我第一次使用C ++中的SDL2.0进行游戏,在尝试使用GameObject类的所有类实例制作矢量时遇到了一个问题,该类包括所有敌人以及播放器角色,并且在类的构造函数中,我所做的是将正在创建的实例添加到vector中,但是它不起作用,因为它给了我一个错误
“严重性代码描述项目文件行抑制状态 错误LNK2001无法解析的外部符号“ public:静态类 std :: vector> GameObject :: allEntities“ (?allEntities @ GameObject @@ 2V?$ vector @ VGameObject @@ V?$ allocator @ VGameObject @@@ std @@@ std @@ A)memory22 C:\ Users \ Marse \ source \ repos \ memory22 \ memory22 \ gameObject .obj 1“
class GameObject {
public:
GameObject(const char* texturesheet, SDL_Renderer* ren, int x, int y, int _maxHp, int _currentHp, int _strength);
~GameObject();
static std::vector<GameObject> allEntities;
...
}
在另一个文件中:
GameObject::GameObject(const char* texturesheet, SDL_Renderer* ren, int xx, int yy, int _maxHp, int _currentHp, int _strength)
{
allEntities.push_back(*this); //If I delete this line it works fine, but i need it to add the class instance being created to the vector
renderer = ren;
objectTexture = textureCreator::loadTexture(texturesheet, ren);
GameObject::maxHp = _maxHp;
GameObject::currentHp = _currentHp;
x = xx;
y = yy;
}