error: invalid use of incomplete type 'struct Foo'

I've been looking at this error for a while and I can´t see where it fails. These are the errors I get:

1
2
C:\Users\Héctor\Documents\Visual Studio 2010\Projects\GekkoEngine\src\Components\../../include/Cube.hpp:12: error: invalid use of incomplete type 'struct Component'
C:\Users\Héctor\Documents\Visual Studio 2010\Projects\GekkoEngine\src\Components\../../include/GameScreen.hpp:14: error: forward declaration of 'struct Component'


GameScreen
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef GAMESCREEN_H
#define GAMESCREEN_H

#include <string>
#include <list>
#include <iostream>
#include <functional>
#include <algorithm>
#include "Util.hpp"
#include "object.hpp"
#include "Component.hpp"

class Engine;
class Component;    // Line 14

class GameScreen : public object
{

    public:
        GameScreen(std::string);

        void setEngine(Engine *engine);
        Engine* getEngine(void);

        void setName(std::string);
        string getName(void);

        void AddComponent(Component*);
        void RemoveComponent(Component*);
        void PutComponentInOrder(Component*);

        void LoadGameScreen(void);

        virtual void Draw(void);
        virtual void Update(void);

        // Sobrescribimos el operador == para poder hacer nuestra propia comparacion
        // friend: no pertenece a la clase, pero puede usar sus miembros
        friend bool operator == (const GameScreen &gs1, const GameScreen &gs2);
        GameScreen& operator = (const GameScreen &gs);

    protected:
        virtual void Load(void);

    private:

        string name;

        static Engine* engine;

        std::list<Component*> components;

        bool loaded;


};

struct CompareGS : public std::binary_function < GameScreen*, GameScreen*, bool>
        {
            bool operator() (const GameScreen* gs1, const GameScreen* value) const
            {
                return (gs1 == value);
            }
        };

#endif // GAMESCREEN_H 


Cube

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
28
29
30
31
32
33
34
#ifndef CUBE_H
#define CUBE_H

#include "../glm/gtc/matrix_transform.hpp"
#include "../glm/gtc/type_ptr.hpp"
#include "../include/GraphicsUtil.hpp"
#include "Component.hpp"
#include "GameScreen.hpp"

class Cube : public Component
{                                                       // Line 12
    public:
        Cube();
        void Update(void);
        void Draw(void);
    protected:
        void Initialize(void);
        void Load(void);
    private:
        static Mesh* mesh;

        static Shader* shader;

        static GLuint
                        ShaderIds[3],
                        TextureId;

        static  GLuint  ModelMatrixUniformLocation,
                        Tex0Loc;

        static mat4 	ModelMatrix;
};

#endif // CUBE_H 
Is the class definition of Component inside Component.hpp?
No, the class definition of Component is in Component.cpp
Please show us component.hpp.

BTW, I think Peter87 meant to ask if the class declaration for Component was in component.hpp. If the declaration for Component is in component.cpp, that's your problem. You can't inherit an incomplete (forwarded) class.


Last edited on
Component.hpp

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef COMPONENT_H
#define COMPONENT_H

#include <string>
#include <map>
#include <typeinfo>
#include <sstream>
#include "Engine.hpp"
#include "GameScreen.hpp"
#include "object.hpp"

class GameScreen;
class Component : public object
{
    public:
        Component();
        virtual ~Component();

        bool getService(void);
        void setService(bool,int);

        bool getVisible(void);
        void setVisible(bool);

        std::string getPathfile(void);
        void setPathfile(std::string);

        std::string getName(void);
        void setName(std::string);

        GameScreen* getParent(void);
        void setParent(GameScreen*);

        int getDrawOrder(void);
        void setDrawOrder(int);

        bool getLoaded(void);
        void setLoaded(bool);

        void LoadComponent(void);

        virtual void Update(void);

        virtual void Draw(void);

        friend bool operator == (const Component &cpt1, const Component &cpt2);
        Component& operator = (const Component &cpt);
        std::string getType(void) const;

    protected:
        virtual void Initialize(void);
        virtual void Load(void);

    private:
        std::string name;

        static GameScreen* parent;

        static std::map<std::string, int> componentTypeCounts;

        // numero de fila de la pila de dibujado de Components
        int drawOrder;

        // Para comprobar si se ha cargado el Component
        bool loaded;

        // Para saber si es un servicio(MIRAR LOS SERVICIOS)
        bool isService;

        // Para saber si el Component debe dibujarse
        bool isVisible;

        // ruta del archivo, si el Component lo tiene
        std::string pathFile;

        // Genera un nombre unico para el componente
        virtual void generateUniqueName(void);
};

struct CompareCPT : public std::binary_function < Component*, Component*, bool>
        {
            bool operator() (const Component* cpt1, const Component* value) const
            {
                return (cpt1 == value);
            }
        };



#endif // COMPONENT_H 

AbstractionAnon wrote:
BTW, I think Peter87 meant to ask if the class declaration for Component was in component.hpp.

No I mean class definition. What he has in Component.hpp is the class definition.

@ShotoReapre
You have a circular dependency. Component.hpp and GameScreen.hpp both include each other. They shouldn't have to include each other. The forward delectations should be all they need. I don't think it will cause an error but maybe you have other circular dependencies that cause this error you are having.
Last edited on
The problem is I need this circular dependencie: Component class have a GameScreen variable ' Parent' and GameScreen class have a std::list<Components>
In your code it is std::list<Component*>. It is just pointers so a forward declaration of Component is all that's needed. Include component.hpp in GameScreen.cpp instead.
Last edited on
nothing, I still have the same problem...
I'm curious why the complaint is about a struct Component, when the lines you indicate are class Component

Is there a struct Component about somewhere?

Andy

PS and while the file is in the subfolder of a folder called Visual Studio 2010, the error messages don't look like they come from VC++??
Last edited on
I don´t have any struct ' Component '.

I use CodeBlocks, but the project is save in Visual Studio 2010 folder
The error appear when I include "cube.hpp" in engine.hpp
I followed the advice of the link, but now I get this error:

1
2
C:\Users\Héctor\Documents\Visual Studio 2010\Projects\GekkoEngine\src\Components\Component.cpp:29: error: invalid use of incomplete type 'struct GameScreen'
C:\Users\Héctor\Documents\Visual Studio 2010\Projects\GekkoEngine\src\Components\../../include/Component.hpp:12: error: forward declaration of 'struct GameScreen'


Component.hpp is the same as above.

Component.cpp

1
2
3
4
5
6
7
8
9
void Component::setService(bool is_service,int Service)
{
    isService = is_service;
    if(isService)
    {
        parent->engine->services.AddService(Service, this); //line 29
    }

}


As engine is a pointer I add the forward declaration 'class Engine;' above 'class GameScreen;' in Component.hpp but it doesn´t works and give me the same error
Last edited on
You are using it in `Component.cpp'
So in `Component.cpp' you must have #include "GameScreen.hpp"

But you are not using it in `Component.hpp', just holding a pointer.
So a forward declare is enough.
Thanks!! Its works! I put the libraries in the files where used and not just files. hpp
Topic archived. No new replies allowed.