C++: syntax for accessing member struct from pointer to class

后端 未结 5 1894
醉话见心
醉话见心 2021-01-18 01:16

I\'m trying to access a member structs variables, but I can\'t seem to get the syntax right. The two compile errors pr. access are: error C2274: \'function-style cast\' : i

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-18 01:36

    You are only declaring Foo::Bar but you don't instantiate it (not sure if that's the correct terminology)

    See here for usage:

    #include 
    
    using namespace std;
    
    class Foo
    {
        public:
        struct Bar
        {
            int otherdata;
        };
        Bar bar;
        int somedata;
    };
    
    int main(){
        Foo::Bar bar;
        bar.otherdata = 6;
        cout << bar.otherdata << endl;
    
        Foo foo;
        //foo.Bar.otherdata = 5;
        foo.bar.otherdata = 5;
    
        //cout << foo.Bar.otherdata;
        cout << foo.bar.otherdata << endl;
    
        return 0;
    }
    

提交回复
热议问题