class is locked?

Hello, I have a problem using class in Visual C++, when I use the int health; from my class Ogre in main() I get the message Ogre::health is inaccessible :( and when I type a. in my main() and hover over it, it gives me a picture with the int health which has a big lock on it, pls help, how do I make my class unlocked?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include <iostream>

class Ogre
{
	int health;
};



int main()
{
	Ogre a;

	a.health;

	return 0;
}
Last edited on
I don't know what you mean by locked, but your Ogre class lacks any access modifiers so it's members will default to private and therefore can't be accessed outside of the class, to make health public:

1
2
3
4
class Ogre{
    public:
        int health;
};
That's one method, but I prefer a slightly different one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Ogre{
    int health;
public:
    Ogre(){health = 20;}
    void health();
};

void Ogre::health(){
    int i;
    cout << "Current health: " << health << endl;
    cout << "Enter new health: ";
    cin >> i;
    health = i;
}

int main(){
    Ogre a;
    a.health();
    return 0;
}
oh, really?
in a lesson I saw someone making a class without using public: and it worked perfectly fine for them.

oh well thanks for the Information :)
yay! it worked!
Last edited on
Topic archived. No new replies allowed.