我在哪里定义内部结构静态对象?我似乎无法做到

时间:2017-08-28 13:13:26

标签: c++ class static definition

我知道我必须在全局范围/命名空间中定义类外部的静态类成员,否则编译器会抱怨缺少符号。所以在下面我不能设法定义内部结构:

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

我还尝试了一个前面的声明,但它说了一些关于重新定义的内容。其实我也想知道,这必须重新定义每一个

1 个答案:

答案 0 :(得分:5)

这是因为全局命名空间中没有这样的符号MyStruct,它位于MyStruct::MyInnerStruct MyStruct::object1; 内。即你需要做什么

alert.addAction

这与在类外部定义成员函数的方式非常相似。