A public struct inside a class
Posted
by Koning Baard
on Stack Overflow
See other posts from Stack Overflow
or by Koning Baard
Published on 2010-05-30T13:12:55Z
Indexed on
2010/05/30
13:22 UTC
Read the original article
Hit count: 222
I am new to C++, and let's say I have two classes: Creature
and Human
:
/* creature.h */
class Creature {
private:
public:
struct emotion {
/* All emotions are percentages */
char joy;
char trust;
char fear;
char surprise;
char sadness;
char disgust;
char anger;
char anticipation;
char love;
};
};
/* human.h */
class Human : Creature {
};
And I have this in my main function in main.cpp
:
Human foo;
My question is: how can I set foo's emotions? I tried this:
foo->emotion.fear = 5;
But GCC gives me this compile error:
error: base operand of '->' has non-pointer type 'Human'
This:
foo.emotion.fear = 5;
Gives:
error: 'struct Creature::emotion' is inaccessible
error: within this context
error: invalid use of 'struct Creature::emotion'
Can anyone help me? Thanks
P.S. No I did not forget the #include
s
© Stack Overflow or respective owner