我知道我必须在全局范围/命名空间的类之外定义一个静态类成员,否则编译器会提示缺少符号。所以在下面我无法定义内部结构:
struct MyStruct
{
struct MyInnerStruct
{
int notimportant1, notimportant2;
};
static MyInnerStruct object1;
};
int main(void)
{
MyStruct foo;
foo.object1.notimportant1 = 5; // Unresolved external symbol, I expected this to happen
return 0;
}
MyInnerStruct MyStruct::object1; // Typical definition of a static member
// MyInnerStruct is underlined, saying it's undefined
我也试过它上面的前向声明,但它说了一些关于重新定义的事情。其实我也想知道,这不得不重新定义每一个
最佳答案
因为在全局命名空间中没有MyInnerStruct
这样的符号,它在MyStruct
里面。 IE。你需要做的
MyStruct::MyInnerStruct MyStruct::object1;
这与您在类外部定义成员函数的方式非常相似。
关于c++ - 在哪里定义内部结构静态对象?好像不能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45919940/