PROBLEM HAS BEEN SOLVED
I'm having some major issues with using structs in other classes. I'm using an IDE that uses header and source files for different classes, but I don't think that's the problem even though declaring certain things is different.
I have two classes, one is main and another is creature.
In the Creature class, I have a struct called "gameObject" and an object of said struct called obj_player.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
- Creature.h -
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class Creature
{
public:
Creature();
void update();
void playerInput();
struct gameObject; // <--
enum dir{LEFT,RIGHT};
private:
};
|
- Creature.cpp - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
struct Creature::gameObject // <--
{
int x, y, // position
height, width, // virtual width and height (mostly used as reference for box collider)
box1, boy1, box2, boy2; // virtual Box Collider
dir facing; // direction the game object is facing
};
Creature::gameObject obj_player = {0,0,32,32,0,0,0,0}; // create a Player game object <--
Creature::Creature()
{
obj_player.facing = RIGHT;
}
|
----------------------------------------------------------------------------------------------------------------------------------------------------------------
In my main class, I create an object of Creature class called creature. and then try to change obj_player's x value to 42
----------------------------------------------------------------------------------------------------------------------------------------------------------------
- main.cpp -
1 2 3 4 5
|
int main()
{
Creature creature; // <--
creature.obj_player.x = 42; // <--
}
|
----------------------------------------------------------------------------------------------------------------------------------------------------------------
When I do this, I receive an error!
"error: 'class Creature' has no member named 'obj_player'"
----------------------------------------------------------------------------------------------------------------------------------------------------------------
I could have this whole thing wrong. Say you were to have a different class and want to use structs to create virtual game objects, then use them in main. How would you do it?
Heck if you could, I'm going to have a game with multiple enemies inside Creature as well, what would be a better way to hold all of their data including the players? I'm still pretty new to making games in C++ and the only teacher is me. I often find myself a little confused in just finding a method in which to do things and get road blocked when I run into errors like these. I just can't seem to find the right way to do anything.