gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:39:13 24 4
gpt4 key购买 nike

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

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_; };
}

它主要由 getter/setter 组成。

它们在 main 的其他地方设置,例如:

Team::setTeams(&activeTeams);

但是我得到了错误:

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

我怀疑是因为 teams_ vector 未初始化,但作为一个抽象类,它不应该被初始化或调用它的构造函数。我该怎么办?

最佳答案

因为您在类声明中有一个static 模板,所以您应该像这样在您的源代码(又名.cpp)上初始化它。

vector* Team::teams_ = new vector<Team>();

关于c++ - 抽象类上未解析的外部符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55196670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com