Page 1 of 1
Struct inheritance problem
Posted: Wed May 09, 2012 12:20 pm
by Blackflower-1996
so, i am having an erron that sais:
Multiple Definition of "Entity" (main.cpp)
First Defined in here (player.cpp)
so my entity struct looks like this:
Code: Select all
#ifndef ENTITY.H
#define ENTITY.H
struct Entity
{
int x,y,w,h;
int getX() { return x;}
int getY() { return y;}
int getW() { return w;}
int getH() { return h;}
void setX(int X){x = X;}
} Entity1;
#endif
then in my class player wich inherit from the entity structs looks like this
Code: Select all
#ifndef PLAYER.H
#define PLAYER.H
#include <SDL/SDL.h>
#include "Entity.h"
class cPlayer: public Entity
{
private:
public:
cPlayer(SDL_Rect *rect);
void PlayerShow(SDL_Surface* src, SDL_Rect *dst,SDL_Surface *screen);
};
#endif
and the error (First defined here) is in the player.cpp as soon as define the construtor
Code: Select all
#include <SDL/SDL.h>
#include "Player.h"
#include "Engine.h"
#include <SDL/SDL.h>
cPlayer::cPlayer(SDL_Rect *rect) // this is the line where it sais it was first defined
{
rect->x = getX();
rect->y = getY();
rect->w = getW();
rect->h = getH();
}
cEngine engine;
void cPlayer::PlayerShow(SDL_Surface *src, SDL_Rect *dst,SDL_Surface *screen)
{
engine.RenderDynamicSurface(src,NULL,screen,dst);
}
any suggestions ??? , thanx for reading
Re: Struct inheritance problem
Posted: Wed May 09, 2012 1:24 pm
by lalacomun
arent you including a source file (.cpp) in you main program ??
Re: Struct inheritance problem
Posted: Wed May 09, 2012 1:25 pm
by Blackflower-1996
lalacomun wrote:arent you including a source file (.cpp) in you main program ??
No
any other suggestions??
Re: Struct inheritance problem
Posted: Wed May 09, 2012 1:32 pm
by Falco Girgis
You havent posted enough for anybody to help you. Where is your main? And can we see the ACTUAL error?
Re: Struct inheritance problem
Posted: Wed May 09, 2012 1:44 pm
by Blackflower-1996
Falco Girgis wrote:You havent posted enough for anybody to help you. Where is your main? And can we see the ACTUAL error?
sure
errors:
obj\Debug\main.o||In function `SDL_main':|
C:\Users\martin\Desktop\Engine\main.cpp|22|multiple definition of `Entity1'|
obj\Debug\Player.o:C:\Users\martin\Desktop\Engine\Player.cpp|6|first defined here|
||=== Build finished: 2 errors, 14 warnings ===|
main.cpp:
Code: Select all
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_audio.h>
#include <SDL/SDL_mixer.h>
#include "Engine.h"
#include "Player.h"
SDL_Surface *screen;
SDL_Surface *player;
SDL_Surface *background;
SDL_Surface *text;
TTF_Font* font;
SDL_Rect rect1;
bool gamerunning = true;
int i[4] = {0,0,0,0};
int main (int argc,char* argv[])
{
cEngine engine;
Entity rectang;
cPlayer player1(&rect1);
Entity1.setX(20);
engine.InitVideo(&screen);
Mix_Music* music;
TTF_Font* font;
SDL_Color color = {0,255,0};
SDL_Rect rect;
rect.x = 20;
rect.y = 70;
rect.w = 40;
rect.h = 80;
engine.InitSound();
engine.LoadMusic(&music,"lolo.ogg");
engine.PlayMusic(music,0);
engine.NewFont(&font,"C:\\Users\\martin\\Desktop\\Engine\\Engine\\Fonts\\ariali.ttf");
engine.RenderText(&text,font,"Demo Test: Graphics, Audio, Text, Input",&color);
engine.LoadBMP(&player,"C:\\Users\\martin\\Desktop\\Engine\\Engine\\Images\\player.bmp");
engine.LoadBMP(&background,"C:\\Users\\martin\\Desktop\\Engine\\Engine\\Images\\background.bmp");
while(gamerunning)
{
engine.HandleInput();
if(i[0] == 1)
rect.y --;
if(i[1] == 1)
rect.x --;
if(i[2] == 1)
rect.y ++;
if(i[3] == 1)
rect.x ++;
if(i[3] == 0)
rect.x = rect.x;
rect.y = rect.y;
engine.RenderStaticSurface(0,0,background,screen,NULL);
engine.RenderDynamicSurface(player,NULL,screen,&rect);
engine.RenderStaticSurface(20,20,text,screen,NULL);
player1.PlayerShow(player,&rect2,screen);
engine.Sync(screen);
}
engine.DeleteMusic(music);
engine.CloseSound();
engine.EndVideo(screen);
engine.StopEngine();
return 0;
}
Re: Struct inheritance problem
Posted: Wed May 09, 2012 2:33 pm
by JarrodParkes
Where is Entity1 created?
Re: Struct inheritance problem
Posted: Wed May 09, 2012 2:55 pm
by Blackflower-1996
JarrodParkes wrote:
Where is Entity1 created?
at the end of struct entity
Re: Struct inheritance problem
Posted: Wed May 09, 2012 3:02 pm
by tappatekie
Blackflower-1996 wrote:JarrodParkes wrote:
Where is Entity1 created?
at the end of struct entity
Your calling a method within a value type with no object reference? (your trying to call a static function...)
EDIT: Don't matter... Don't know much c++ :L
Re: Struct inheritance problem
Posted: Wed May 09, 2012 3:21 pm
by Nokurn
You're defining an Entity1 variable every time Entity.h is included, as the definition is in the header itself. Do not define variables in headers; declaring them is ok if you don't mind global variables, but defining them is universally wrong.
If you want a global Entity1 variable, you must
declare it in a header (such as Entity.h) like so:
(note the
extern directive)
Then
define it in a
single source file (like Entity.cpp):
Code: Select all
#include "Entity.h"
Entity Entity1;
Definitions should only exist in one translation unit, so that only one definition exists once all of the object files are linked.
Also, I don't recommend putting periods in preprocessor constant names. Consider using underscores instead.
Re: Struct inheritance problem
Posted: Wed May 09, 2012 7:41 pm
by Falco Girgis
^ Yes. Everything.