Struct inheritance problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Blackflower-1996
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Mon May 07, 2012 2:25 pm

Struct inheritance problem

Post 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 ;)
User avatar
lalacomun
VS Setup Wizard
VS Setup Wizard
Posts: 114
Joined: Wed Dec 28, 2011 10:18 pm
Favorite Gaming Platforms: psx, nintendo ds, gameboy advance, xbox 360, ps2
Programming Language of Choice: C++
Location: Argentina
Contact:

Re: Struct inheritance problem

Post by lalacomun »

arent you including a source file (.cpp) in you main program ?? ;)
Image
Blackflower-1996
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Mon May 07, 2012 2:25 pm

Re: Struct inheritance problem

Post by Blackflower-1996 »

lalacomun wrote:arent you including a source file (.cpp) in you main program ?? ;)
No :( any other suggestions??
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Struct inheritance problem

Post by Falco Girgis »

You havent posted enough for anybody to help you. Where is your main? And can we see the ACTUAL error?
Blackflower-1996
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Mon May 07, 2012 2:25 pm

Re: Struct inheritance problem

Post 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;
}
User avatar
JarrodParkes
ES Beta Backer
ES Beta Backer
Posts: 325
Joined: Thu Feb 25, 2010 2:39 pm
Current Project: Yes, iPhone Application(s)
Favorite Gaming Platforms: N64, PC
Programming Language of Choice: C++
Location: Mountain View, CA
Contact:

Re: Struct inheritance problem

Post by JarrodParkes »

Code: Select all

Entity1.setX(20);
Where is Entity1 created?
Blackflower-1996
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Mon May 07, 2012 2:25 pm

Re: Struct inheritance problem

Post by Blackflower-1996 »

JarrodParkes wrote:

Code: Select all

Entity1.setX(20);
Where is Entity1 created?
at the end of struct entity

Code: Select all

struct Entity
{
     ...
} Entity1;

tappatekie
Chaos Rift Junior
Chaos Rift Junior
Posts: 204
Joined: Mon Nov 21, 2011 3:01 pm
Current Project: Web browser from scratch
Favorite Gaming Platforms: SNES, PSP, PS1 and 3
Programming Language of Choice: C#
Location: A house near me
Contact:

Re: Struct inheritance problem

Post by tappatekie »

Blackflower-1996 wrote:
JarrodParkes wrote:

Code: Select all

Entity1.setX(20);
Where is Entity1 created?
at the end of struct entity

Code: Select all

struct Entity
{
     ...
} Entity1;

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
Last edited by tappatekie on Wed May 09, 2012 3:56 pm, edited 1 time in total.
User avatar
Nokurn
Chaos Rift Regular
Chaos Rift Regular
Posts: 164
Joined: Mon Jan 31, 2011 12:08 pm
Favorite Gaming Platforms: PC, SNES, Dreamcast, PS2, N64
Programming Language of Choice: Proper C++
Location: Southern California
Contact:

Re: Struct inheritance problem

Post 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:

Code: Select all

extern Entity Entity1;
(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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Struct inheritance problem

Post by Falco Girgis »

^ Yes. Everything. :)
Post Reply