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:
...
};
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.
#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.
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.