Reputation: 10137
I'm defining a C++ header file, and for some reason the class I'm creating gives an error when I try to refer to a struct which is defined in the very same file, along with an enumeration class I created.
I'm pretty to new to C++, though I have some experience with Java and C#. Even then, my programming experience is relatively low. Am I initializing the reference wrong? Should I be placing both the struct and the enum in a separate header file?
#include <iostream>
#include <stdio.h>
class Character
{
private:
Stats stats; //<--error: "Type 'Stats' could not be resolved."
public:
};
struct Stats
{
int strength;
int intelligence;
int endurance;
int speed;
int agility;
int luck;
};
enum Race
{
NONE,
HUMAN,
ALIEN,
ANDROID
};
Note: I'm using Eclipse 3.7 (Indigo) for C++, in case that means anything.
Upvotes: 0
Views: 252
Reputation: 145359
In C++ you cannot use something unless it has already been declared.
In your case, Stats
must not only have been declared but it must have been completely defined.
That said, ALL UPPERCASE IDENTIFIERS are commonly reserved for macros. Don't use them for constants. Using them for constants is an aggressive attack on the eyes of anyone reading the code, it risks inadvertent text replacement, and it reduces an already small set of choices for macro names. It's a Java-ism. It works in Java because Java does not have a preprocessor (ironically, Java got the convention from C, where it macros were employed as "constants").
Cheers & hth
Upvotes: 1
Reputation: 355157
C++ is parsed from the top of the file to the bottom; you need to move your Stats
class definition to above your Character
class definition.
Upvotes: 4