My struct object in another class doesn't exist?!

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.
Last edited on
closed account (D80DSL3A)
What you have in Creature.h is more like a forward declaration of the gameObject structure defined in the Creature.cpp file.
There is no member called obj_player declared in Creature.h

I would do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Creature
{
    public:
        Creature();
        void update();
        void playerInput();

        struct gameObject // define the structure here
        {
            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
        };

        gameObject obj_player;// now declare an instance of gameObject as a data member of the Creature class.

        enum dir{LEFT,RIGHT};
    private:

};

Does line 10 in Creature.cpp work?
Creature::gameObject obj_player = {0,0,32,32,0,0,0,0}; // create a Player game object <--
You should probably make those assignments in the Creature constructor.

"error: 'class Creature' has no member named 'obj_player'"

This incomplete error message leaves out what line in which file was found to be faulty.
Please post error messages in their entirety.
The error message you ask for was just in a line inside main (line 25, 4 in the example) in which I was asking to change the "x coordinate" inside my gameObject struct.

creature.obj_player.x = 42;

That was the only error, however...

I've done it! Apparently when declaring structs in a header file, you are actually supposed to give it a body along with all of its members rather than declaring it inside of .cpp and .h!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef CREATURE_H
#define CREATURE_H


class Creature
{
    public:
        Creature();
        void update();
        void playerInput();

        struct 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
        };
        gameObject obj_player;

        enum dir{LEFT,RIGHT};
    private:

};

#endif // CREATURE_H 


Now I no longer get any errors which means that it accepts my changing of obj_player inside of main! Thanks dude, you gave me some clues as to what the problem was. Man, forum sites, who knew? XDD
Last edited on
Topic archived. No new replies allowed.