我在哪里定义一个内部结构静态对象?看来我不能

问题描述:

我知道我必须在全局作用域/名称空间的类的外部定义一个静态类成员,否则编译器会抱怨丢失的符号。因此,在下面我不能设法限定内部结构:我在哪里定义一个内部结构静态对象?看来我不能

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之内。即你需要做

MyStruct::MyInnerStruct MyStruct::object1; 

这和你在类之外定义成员函数的方法非常相似。

+1

刚刚添加,看起来像“无法解析的外部符号”与此无关,是一个单独的问题 –