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

后端 未结 5 1896
醉话见心
醉话见心 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:58

    You create a nested structure, but you never create any instances of it within the class. You need to say something like:

    class Foo{
    public:
        struct Bar{
            int otherdata;
        };
        Bar bar;
        int somedata;
    };
    

    You can then say:

    foo.bar.otherdata = 5;
    

提交回复
热议问题