Nested header files error

Hello, I'm having some trouble compiling my program.

I think this is the cause:

Entities.cpp contains #include "pinball.h"

however, pinball.h also contains #include "Entities.cpp"

Entities.cpp
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
#include "pinball.h"

class Entity{

    virtual draw_self();

};

class StaticWall : Entity{

    b2Body physics_body = NULL;

    std::vector<Drawable> drawList;

    StaticWall(b2Vec2 vertices, int vertex_amount){
        b2BodyDef bodyDef;
        bodyDef.type = b2_staticBody;
        bodyDef.position.Set(0,0);

        b2Body* body = world.CreateBody(&bodyDef);

        b2ChainShape chain;
        chain.CreateChain(vertices, vertex_amount);

        b2FixtureDef fixtureDef;
        fixtureDef.shape = &chain;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.3f;
        body->CreateFixture(&fixtureDef);

        for (unsigned i=0, i<vertex_amount-1;i++){
            Drawable_Line line(vertices[i].x,vertices[i].y,vertices[i+1].x,vertices[i+1].y);
            drawList[i]=line;
        }
    }

    draw_self(){
        for (unsigned i=0; i < drawList.size(); i++) {
            drawList[i].draw();
        }
    }

}


pinball.h
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <Box2D/Box2D.h>
#include <vector>
#include "Ball.cpp"
#include "CreatorFunctions.cpp"
#include "tinyxml/tinyxml.h"
#include "Entities.cpp"
#include "Drawables.cpp" 


How Can I prevent this infinite #include loop? (Or is it something else?)
Is there anything else that's really stupid in my code that I should change?
never include .cpp files.
.cpp files are compiled, .h files are not. This is why you include h files into cpp files and not the other way around. bringing cpp files together is the job of the linker, which you need not be too concerned with.
change them to a .h its easier and works better
^ What they said.
Also header guards, and don't include gratuitously http://www.cplusplus.com/forum/articles/10627/
Okay, nested include errors are gone, thanks! (Now on to the next errors :-( )

Speaking of these errors, I'm getting this one:

/home/werner/c++workspace/Pinball/Entities.h|30|error: no match for ‘operator[]’ in ‘vertices[i]’|

part of Entities.h that's causing trouble:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    StaticWall(b2Vec2 vertices, int vertex_amount, b2World & physics_world){
        b2BodyDef bodyDef;
        bodyDef.type = b2_staticBody;
        bodyDef.position.Set(0,0);

        b2Body * body = physics_world.CreateBody(&bodyDef);

        b2ChainShape chain;
        chain.CreateChain(&vertices, vertex_amount);

        b2FixtureDef fixtureDef;
        fixtureDef.shape = &chain;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.3f;
        body->CreateFixture(&fixtureDef);

        for (int i=0; i<vertex_amount-1;i++){
            Drawable_Line line(vertices[i].x,vertices[i].y,vertices[i+1].x,vertices[i+1].y);
            drawList[i]=line;
        }
    }
vectices is just 1 object. I guess that you wanted to pass a vector or an array.
Another error gone! Now, I still have this error that I can't figure out:

/home/werner/c++workspace/Pinball/Entities.h|31|error: no match for ‘operator=’ in ‘((StaticWall*)this)->StaticWall::drawList.std::vector<_Tp, _Alloc>::operator[] [with _Tp = Drawable, _Alloc = std::allocator<Drawable>](((unsigned int)i)) = line’|

What the class currently looks like:
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
class StaticWall : Entity{

    std::vector<Drawable> drawList;

    StaticWall(b2Vec2 vertices[], int vertex_amount, b2World & physics_world){
        b2BodyDef bodyDef;
        bodyDef.type = b2_staticBody;
        bodyDef.position.Set(0,0);

        b2Body * body = physics_world.CreateBody(&bodyDef);

        b2ChainShape chain;
        chain.CreateChain(vertices, vertex_amount);

        b2FixtureDef fixtureDef;
        fixtureDef.shape = &chain;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.3f;
        body->CreateFixture(&fixtureDef);

        for (int i=0; i<vertex_amount-1;i++){
            Drawable_Line line(vertices[i].x,vertices[i].y,vertices[i+1].x,vertices[i+1].y);
            drawList[i] = line; // <-- Compiler error here
        }
    }

    void draw_self(){
        for (unsigned i=0; i < drawList.size(); i++) {
            drawList[i].draw();
        }
    }

}

EDIT: Nevermind, something went wrong so that Drawable_Line was not a child of Drawable. It's working now. Thanks for helping me with those other errors though!
Last edited on
Topic archived. No new replies allowed.