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

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

它主要由 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/

相关文章:

c++ - 使用soxr重采样时发生访问冲突

c++ - 禁用 QtCreator 中的特定警告

c++ - 当名称出现在函数的 declarator-id 之前时,查找规则是什么?

c++ - Qt-有没有办法优先调整子窗口小部件的大小

c++ - Boost.Asio SSL 上下文未验证证书

c++ - ADL 名称查找问题,正在使用 std::swap; swap(a,b) 与函数重载或内部作用域函数隐藏外部作用域函数有关?

c++ - Linux,在fork()中与另一个程序共享一个缓冲区

c++ - 将 Visual Studio 项目设置复制到新项目吗?

c++ - 重载指向重载函数的指针

c++ - 你如何在 C++ 中将指数与变量一起使用?