Error for static variable under private section

P

Pallav singh

Hi All

why i am getting error for static variable under private section

class Test
{
private :
static int s_private_int;
public:
static int s_public_int;
};

int main()
{
Test::s_public_int = 145; // OK
Test::s_private_int = 12; // wrong, don't touch
// the private parts
return 0;
}

Thanks
 
P

peter koch

Hi  All

why i am getting error for static variable under private section

class Test
    {
    private :
        static int s_private_int;
    public:
        static int s_public_int;
    };

    int main()
    {
        Test::s_public_int = 145;   // OK
        Test::s_private_int = 12;   // wrong, don't touch
                                    // the private parts
        return 0;
    }

Well... one likely reason could be that it is private? Please realise
that Test::s_private_int = 12 is an assignment, not an initialisation.

/Peter
 
A

Alf P. Steinbach

* Pallav singh:
why i am getting error for static variable under private section

class Test
{
private :
static int s_private_int;
public:
static int s_public_int;
};

int main()
{
Test::s_public_int = 145; // OK
Test::s_private_int = 12; // wrong, don't touch
// the private parts
return 0;
}

Probably your compiler has already told you.

But anyways, you can define a class' static variables locally in some function.
They must be defined in the class' namespace. Which here is the global namespace.


Cheers, & hth.,

- Alf
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* Pallav singh:

Probably your compiler has already told you.

But anyways, you can define a class' static variables locally in some
function. They must be defined in the class' namespace. Which here is
the global namespace.

Oh, sorry, on second reading it seem you're not trying to define the variables
at all, just assign to them.

Well, you need to define them first, that's one thing.

Second, and what I now perceive to be your main query: to find out the meaning
of "private" and other such basic stuff, consult your textbook. Which textbook
are you using? If it doesn't explain "private", then tell us and burn it.


Cheers, & hth.,

- Alf
 
S

songli9447

You should change your code as follows:

class Test
{
private :
static int s_private_int;
public:
static int s_public_int;
};

int Test::s_public_int = 145; // OK
int Test::s_private_int = 12; // wrong, don't touch
// the private parts
int main()
{

return 0;
}

Maybe now you know why it caused error~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top