boshkodj
8.7.2009, 0:15
Poceo sam da pravim engine u SDL-u i naisao sam na jedan veoma cudan problem. Imam jedan game.h fajl u kome se nalazi klasa ,i game.cpp u kome sam "deklarisao" tu klasu. I onda mi se pojave dosta cudne greske :
main.obj : error LNK2005: "class Sprite pozadina" (?pozadina@@3VSprite@@A) already defined in Game.obj
main.obj : error LNK2005: "public: void __thiscall Sprite::Render(int,int,struct SDL_Surface *)" (?Render@Sprite@@QAEXHHPAUSDL_Surface@@@Z) already defined in Game.obj
main.obj : error LNK2005: "public: void __thiscall Sprite::Get_Picture(char const *)" (?Get_Picture@Sprite@@QAEXPBD@Z) already defined in Game.obj
main.obj : error LNK2005: "void __cdecl apply_surface(int,int,struct SDL_Surface *,struct SDL_Surface *,struct SDL_Rect *)" (?apply_surface@@YAXHHPAUSDL_Surface@@0PAUSDL_Rect @@@Z) already defined in Game.obj
main.obj : error LNK2005: "struct SDL_Surface * __cdecl load_image(char const *)" (?load_image@@YAPAUSDL_Surface@@PBD@Z) already defined in Game.obj
C:\Documents and Settings\korisnik\My Documents\Visual Studio 2008\Projects\SDL - vezbe\Release\SDL - vezbe.exe : fatal error LNK1169: one or more multiply defined symbols found
Build log was saved at "file://c:\Documents and Settings\korisnik\My Documents\Visual Studio 2008\Projects\SDL - vezbe\SDL - vezbe\Release\BuildLog.htm"
Ove greske mi se pojave kad imam game.cpp i game.h ,ali mi se ne pojave kad je klasa deklarisana u game.h
Evo vec sav kod tog engine - a:
Event.h:
#ifndef _EVENT_H
#define _EVENT_H
#include "SDL.h"
SDL_Event event;
#endif
Image.h:
#ifndef _IMAGE_H
#define _IMAGE_H
#include "SDL.h"
#include "SDL_image.h"
SDL_Surface *load_image( const char *name )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( name );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
if( optimizedImage != NULL )
{
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
}
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination , SDL_Rect *clip = NULL )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source,clip , destination, &offset );
}
#endif
Sprite.h:
#ifndef _SPRITE_H
#define _SPRITE_H
#include "Image.h"
class Sprite
{
private:
SDL_Surface *picture ;
public:
void Get_Picture(const char *path);
void Render(int X_pos,int Y_pos,SDL_Surface *render_screen);
};
void Sprite::Get_Picture(const char *path)
{
picture = load_image(path);
}
void Sprite::Render(int X_pos, int Y_pos, SDL_Surface *render_screen)
{
apply_surface(X_pos,Y_pos,picture,render_screen);
}
#endif
game.h
#ifndef _GAME_H
#define _GAME_H
#include "Image.h"
#include "Sprite.h"
Sprite pozadina ;
class Game
{
private:
SDL_Surface *screen ;
int x ;
int y ;
public:
void GameStart(int X,int Y) ;
void GameLoop();
void GameEnd();
};
#endif
game.cpp
#include "Game.h"
#include "Sprite.h"
void Game::GameStart(int X,int Y)
{
x = X;
y = Y;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(x,y,32,SDL_SWSURFACE);
pozadina.Get_Picture("pozadina.bmp");
}
void Game::GameLoop()
{
pozadina.Render(0,0,screen);
SDL_Flip(screen);
}
void Game::GameEnd()
{
SDL_Quit();
}
main.cpp
#include "Event.h"
#include "Image.h"
#include "Sprite.h"
#include "Game.h"
bool quit = false ;
Game game;
int main(int argc,char **argv)
{
game.GameStart(640,480);
while( quit == false )
{
while( SDL_PollEvent(&event) )
{
if(event.type == SDL_QUIT)
{
quit = true;
}
}
game.GameLoop();
}
game.GameEnd();
return 0;
}
E kad ovaj kod kompajliram pojave mi se greske koje sam naveo gore,a kada je kod ovakav:
game.h:
#ifndef _GAME_H
#define _GAME_H
#include "Image.h"
#include "Sprite.h"
Sprite pozadina ;
class Game
{
private:
SDL_Surface *screen ;
int x ;
int y ;
public:
void GameStart(int X,int Y) ;
void GameLoop();
void GameEnd();
};
void Game::GameStart(int X,int Y)
{
x = X;
y = Y;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(x,y,32,SDL_SWSURFACE);
pozadina.Get_Picture("pozadina.bmp");
}
void Game::GameLoop()
{
pozadina.Render(0,0,screen);
SDL_Flip(screen);
}
void Game::GameEnd()
{
SDL_Quit();
}
#endif
,tada se ne pojavi ni jedna greska.
Molim vas pomozite! :(
main.obj : error LNK2005: "class Sprite pozadina" (?pozadina@@3VSprite@@A) already defined in Game.obj
main.obj : error LNK2005: "public: void __thiscall Sprite::Render(int,int,struct SDL_Surface *)" (?Render@Sprite@@QAEXHHPAUSDL_Surface@@@Z) already defined in Game.obj
main.obj : error LNK2005: "public: void __thiscall Sprite::Get_Picture(char const *)" (?Get_Picture@Sprite@@QAEXPBD@Z) already defined in Game.obj
main.obj : error LNK2005: "void __cdecl apply_surface(int,int,struct SDL_Surface *,struct SDL_Surface *,struct SDL_Rect *)" (?apply_surface@@YAXHHPAUSDL_Surface@@0PAUSDL_Rect @@@Z) already defined in Game.obj
main.obj : error LNK2005: "struct SDL_Surface * __cdecl load_image(char const *)" (?load_image@@YAPAUSDL_Surface@@PBD@Z) already defined in Game.obj
C:\Documents and Settings\korisnik\My Documents\Visual Studio 2008\Projects\SDL - vezbe\Release\SDL - vezbe.exe : fatal error LNK1169: one or more multiply defined symbols found
Build log was saved at "file://c:\Documents and Settings\korisnik\My Documents\Visual Studio 2008\Projects\SDL - vezbe\SDL - vezbe\Release\BuildLog.htm"
Ove greske mi se pojave kad imam game.cpp i game.h ,ali mi se ne pojave kad je klasa deklarisana u game.h
Evo vec sav kod tog engine - a:
Event.h:
#ifndef _EVENT_H
#define _EVENT_H
#include "SDL.h"
SDL_Event event;
#endif
Image.h:
#ifndef _IMAGE_H
#define _IMAGE_H
#include "SDL.h"
#include "SDL_image.h"
SDL_Surface *load_image( const char *name )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( name );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
if( optimizedImage != NULL )
{
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
}
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination , SDL_Rect *clip = NULL )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source,clip , destination, &offset );
}
#endif
Sprite.h:
#ifndef _SPRITE_H
#define _SPRITE_H
#include "Image.h"
class Sprite
{
private:
SDL_Surface *picture ;
public:
void Get_Picture(const char *path);
void Render(int X_pos,int Y_pos,SDL_Surface *render_screen);
};
void Sprite::Get_Picture(const char *path)
{
picture = load_image(path);
}
void Sprite::Render(int X_pos, int Y_pos, SDL_Surface *render_screen)
{
apply_surface(X_pos,Y_pos,picture,render_screen);
}
#endif
game.h
#ifndef _GAME_H
#define _GAME_H
#include "Image.h"
#include "Sprite.h"
Sprite pozadina ;
class Game
{
private:
SDL_Surface *screen ;
int x ;
int y ;
public:
void GameStart(int X,int Y) ;
void GameLoop();
void GameEnd();
};
#endif
game.cpp
#include "Game.h"
#include "Sprite.h"
void Game::GameStart(int X,int Y)
{
x = X;
y = Y;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(x,y,32,SDL_SWSURFACE);
pozadina.Get_Picture("pozadina.bmp");
}
void Game::GameLoop()
{
pozadina.Render(0,0,screen);
SDL_Flip(screen);
}
void Game::GameEnd()
{
SDL_Quit();
}
main.cpp
#include "Event.h"
#include "Image.h"
#include "Sprite.h"
#include "Game.h"
bool quit = false ;
Game game;
int main(int argc,char **argv)
{
game.GameStart(640,480);
while( quit == false )
{
while( SDL_PollEvent(&event) )
{
if(event.type == SDL_QUIT)
{
quit = true;
}
}
game.GameLoop();
}
game.GameEnd();
return 0;
}
E kad ovaj kod kompajliram pojave mi se greske koje sam naveo gore,a kada je kod ovakav:
game.h:
#ifndef _GAME_H
#define _GAME_H
#include "Image.h"
#include "Sprite.h"
Sprite pozadina ;
class Game
{
private:
SDL_Surface *screen ;
int x ;
int y ;
public:
void GameStart(int X,int Y) ;
void GameLoop();
void GameEnd();
};
void Game::GameStart(int X,int Y)
{
x = X;
y = Y;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(x,y,32,SDL_SWSURFACE);
pozadina.Get_Picture("pozadina.bmp");
}
void Game::GameLoop()
{
pozadina.Render(0,0,screen);
SDL_Flip(screen);
}
void Game::GameEnd()
{
SDL_Quit();
}
#endif
,tada se ne pojavi ni jedna greska.
Molim vas pomozite! :(