C++ 静态属性

标签 c++ static properties

我在访问类中的静态属性时遇到问题。我收到以下错误:

shape.obj : error LNK2001: unresolved external symbol "public: static class TCollection<class Shape *> Shape::shapes"

类的定义是:

class Shape {

public:
    static Collection<Shape*> shapes;

    static void get_all_instances(Collection<Shape*> &list);
};

静态方法的实现是:

void Shape::get_all_instances(Collection<Shape*> &list) {
    list = Shape::shapes;
}

好像shapes属性未被初始化。

最佳答案

你是对的,因为静态变量只在类中声明而没有定义。

必须也定义它们,只需将以下行添加到您的实现所在的文件中。

Collection<Shape*> Shape::shapes;

它应该可以解决问题。

关于C++ 静态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4007458/

相关文章:

c++ - 在 VS2005 中使用 MSBuild for C++

c++ - 类型删除和分配器 : what's the expected behavior?

c - 静态常量存储在哪里?

objective-c - 什么时候使用@property?

java - `Properties` 是否仍用于从文件加载 JDBC 配置?

c++ - 用于 NTLM 身份验证的 GSSAPI

c++ - 什么是静态变量?

swift - Swift 中的隐式惰性静态成员

vb.net - 无法设置属性的属性

c++ - QObject::connect 中 Func2 类型的参数?