Can't acces structure in class

I have defined stucture in class, example:
1
2
3
4
5
6
7
class my_class
{
    struct my_struct
    {
        int my_var;
    };
};


Then trying to access it:
1
2
3
4
5
my_class
    cclass;

class.my_struct
    sstruct;


But I get this error:
 
error C2274: 'function-style cast' : illegal as right side of '.' operator


What's wrong?
Last edited on
closed account (zb0S216C)
The default class access-specifier is private by default. private basically means than any external modification is disallowed. Adding public: before my_struct will fix the problem. public: indicates that any members below it are accessible to anything outside the class.

References:
Access-Specifiers: http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/


Wazzak
Last edited on
Sorry, I forgot to write, that I have specified 'public'.
closed account (zb0S216C)
Are you trying to instantiate my_struct? If so, then you need to use the scope resolution operator (::). For example:

1
2
3
4
int main( )
{
    my_class::my_struct InstanceName;
}

Here, you're telling the compiler that my_struct resides within my_class, not within an instantiation of my_class. The member operator (.) is used to access members of an instantiated class, not the base class.

Wazzak
Thanks.
Topic archived. No new replies allowed.