抽象类上未解析的外部符号

时间:2019-03-16 12:17:07

标签: c++

我有一个看起来像这样的课:

class Team
{
protected:
    string name_;
    Pixel color_;
    static vector<Team>* teams_;
public:
    string name() { return name_; };
    Pixel color() { return color_; };
    static void setTeams(vector<Team>* t) { teams_ = t; };
    static vector<Team>* teams() { return teams_; };
}

它主要由吸气剂/阻气剂组成。

它们设置在main中的其他位置,例如:

Team::setTeams(&activeTeams);

但是我得到了错误:

unresolved external symbol "public: static class std::vector<class Team,class std::allocator<class Team> > * Team::teams_"

我怀疑是因为teams_向量没有初始化,但是作为抽象类,它永远都不应初始化或调用其构造函数。我该怎么办?

2 个答案:

答案 0 :(得分:0)

具有static的成员变量存在independently from any instances。因此,您必须对其进行初始化。

您的类是抽象的事实(对于您而言,编译器无法知道),不会改变任何事情。

答案 1 :(得分:0)

由于在类声明中有一个static模板,因此应像这样在源代码(也称为.cpp)上对其进行初始化。

vector* Team::teams_ = new vector<Team>();
相关问题