Inhertiance from classes defined in other files

So I have this class client that is defined in client.cpp, and I have another class player, which inherits all of clients classes. The problem, is that when I try to inherit the client class as protected, it tells me that there is no class. So I forward declare the client class at the beginning of the file, and it gives me an error stating that it's an invalid inheritance of an incomplete struct.
Was wondering how to solve this problem, as I want to keep the client and player classes separate.
1
2
3
4
5
6
7
8
#include "client.h"
class client;
class player : protected client{
public:
...
protected:
...
};
Last edited on
closed account (o1vk4iN6)
remove class client; you are redefining the class client to be incomplete
If I do, it tells me that there is no client in the first place, as the header guards against multiple definitions. Also, class client; doesn't redefine it, it only declares it. If I were to do class client{}; then the compiler would hate me. As far as I know.
closed account (o1vk4iN6)
need more code then
1
2
3
4
5
6
class client{
public:
...lalala
protected:
..lalala
};

:P
You really need to show us the client.h file. There is something missing.

A couple things that jumped out, but may not be related to the actual problem.

You are missing a semi-colon after line 8. Did you forget that in the client class, too?

You do not have guards here (#ifndef player_h / #define player_h / #endif). Are you missing these in your client.h file?
closed account (o1vk4iN6)
Major Tom wrote:

:P


ah yes, i see the problem now, this is very helpful
ah yes, i see the problem now, this is very helpful


xD sorry, I was rather confused as to what code you wanted.

Alright, heres client.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
28
29
30
31
32
33
34
#ifndef CLIENT_H
#define CLIENT_H
#include "world.h"
#include "maths.h"
#include "Constructs.h"
#include "player.h"
#include "renderingEngine.h"
#include "SOIL.h"
#include <gl/gl.h>
#include <gl/glut.h>
#include <iostream>
using namespace std;
class renderingEngine;
class player;
class client{
public:
void renderWorld();
client();
client(world *world);
client(player *player);
client(world *world,player *player);
void setWorld(world *world){myWorld = world;}
void setPlayer(player *player){myPlayer=player;}
void loadTexts();
protected:
world *myWorld;
player *myPlayer;
GLuint textureList[10];
renderingEngine *renderer;
cubeLayout *cubeTexture;
int ScreenWidth,ScreenHeight;
};

#endif 

and player.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef PLAYER_H
#define PLAYER_H
#include <gl/gl.h>
#include <gl/glut.h>
#include "camera.h"
#include "maths.h"
#include "world.h"
#include "voxel.h"
#include "time.h"
#include "Constructs.h"
#define CAMERASPEED 0.05
class client;
#include "client.h"
class world;
//Problem of the day: how do I inherit from another class that is defined in another file?
class player {//: protected client {
public:
     player();
//   string name;
     vec3 getCurrentCube(world *);
     void keyboardInput();
     void calculatePhysics(world *);
     vec3 velocity,acceleration;
     vec3 getPlayerPositionRelativeTo(int);
     void setPlayerPositionRelativeTo(int, vec3*);
     bool checkGround(world *pworld);
     void setPosition(vec3);
    protected:
     camera *pCamera;
     int playerId;
     //Physics stuffs
     vec3 lastPosition;
     //Some physics testing stuff
     vec3 position,speed;
     double t;
     double dt;
     double currentTime;
     double accumulator;
     bool collisionDetection;
     //end physics testing stuffs
     vec3 currentForces;
     bool playerJump;
     bool isOnGround;
     vec3 getPosition();
     int height; //in Cubic dimensions bodyPositioning[1] - bodyPositioning[3]
};



#endif 

The reason I'm doing this is to make my whole engine more streamlined, because the way I started coding it (as I'm still getting to know C++) was rather crappy. As time moved on I realized that I needed to revamp the OO, and thus I'm looking into inheritance to make things better.
Last edited on
closed account (o1vk4iN6)
Well that's one problem. The relation between player and client is a bit messy. Why does client have a pointer to a player than have "player" inherit "client" ? I think you need to reason with yourself about more about your decisions.

One solution:

Remove #include "player.h" from client.h and leave the forward declaration of class player;.

Remove the forward declaration class client; from player.h.



Since in client.h you are only using pointers, you don't actually need to know the contents of "player" (only when it is being used in the source, where you can than include the header file), but when you are inheriting a class, you do.
Makes sense, and I did all of that, but the compiler is spewing this error:
error: expected class name before token '{'
closed account (o1vk4iN6)
1
2
3
4
5
6
7
8
9
10
11
class B;

class A
{
    B *b;
};

class B : A
{

};


Compiles for me.
Topic archived. No new replies allowed.