last night my friend send a code for me because he couldn't compile it.I checked the makefile and saw that he wasn't linked one of .o files.I fixed that.then I got multiple definition error.i opened .h files and wrote #ifndef,#define,#endif for all of them.then again i got the multiple definition error.so i opened all 3 .h files and saw something unusual.he included one .cpp file in header of two class.so i searched on Internet and found out that you should never include .cpp file and that caused the problem.but problem is the .cpp file that he included is not a class, it just contains 3 functions and i can't write a header for it and handle it like a class.how i could fix this problem?
global_functions.cpp(the cpp that he included and causing problem)
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
|
#include <string>
#include <iostream>
SDL_Surface *load_image( std::string filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP( filename.c_str() );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, NULL, destination, &offset );
}
bool OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int X2, int Y2, int W, int H)
{
if(Surf_Dest == NULL || Surf_Src == NULL) {
return false;
}
SDL_Rect DestR;
DestR.x = X;
DestR.y = Y;
SDL_Rect SrcR;
SrcR.x = X2;
SrcR.y = Y2;
SrcR.w = W;
SrcR.h = H;
SDL_BlitSurface(Surf_Src, &SrcR, Surf_Dest, &DestR);
return true;
}
|
shower.h(global_functions.cpp included on it)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#ifndef __SHOWER_H
#define __SHOWER_H
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "define.cpp"
#include "global_functions.cpp"
#include <string>
class Shower {
private:
SDL_Surface* screen;
int where; // :|
SDL_Surface* safhe;
int width;
public:
void showMap();
void showMario();
void showEnemies();
void showMashrums();
void show_screen();
void setWidth(int x);
Shower(SDL_Surface* _screen, int _where, int _width);
};
#endif
|
mario.h(other header that global_functions.cpp included on it)
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
|
#ifndef __MARIO_H
#define __MARIO_H
#include <iostream>
#include <string>
#include "define.cpp"
#include "SDL/SDL.h"
#include "map.h"
#include "SDL/SDL_gfxPrimitives.h"
#include <cmath>
#include "SDL/SDL_image.h"
#include "global_functions.cpp"
using namespace std;
struct Point {
double x, y;
};
class Mario {
private:
Point point;
int type;
int mtype;
int life;
Map* map;
double speed;
SDL_Surface* mini, *screen;
int t;
public:
bool can(Point p);
void move(int type);
void gravity();
Mario(int _life, Point _point, Map* _map, SDL_Surface* scr);
bool die();
void fraction();
void show();
int getTime();
void setTime(int _time);
};
#endif
|
main.cpp
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
|
#include "map.h"
#include "shower.h"
#include <iostream>
#include <string>
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "mario.h"
using namespace std;
int main()
{
SDL_Surface* screen = SDL_SetVideoMode(256, 224, 24, SDL_HWSURFACE);
Map* map = new Map;
map->loadMap("world1-1.txt");
Shower* show = new Shower(screen, 0, map->getWidth());
SDL_Event event;
show->showMap();
Point p;
p.x = 176;
p.y = 200;
Mario* mario = new Mario (3, p, map, screen);
while(1)
{
while(SDL_PollEvent( &event ))
{
if(event.type == SDL_QUIT)
return 0;
}
show->show_screen();
mario->setTime(mario->getTime() + 1);
mario->show();
}
}
|
Makefile
all: define.cpp shower.cpp map.cpp main.cpp map.h shower.h shower.o map.o global_functions.cpp mario.o Mario.cpp mario.h
g++ main.cpp shower.o map.o Mario.o -lSDL -lSDL_gfx -lSDL_image -o mario
shower.o: shower.h shower.cpp
g++ shower.cpp -c -lSDL -lSDL_gfx -lSDL_image
map.o: map.h map.cpp
g++ map.cpp -c -lSDL -lSDL_gfx
mario.o: mario.h Mario.cpp
g++ -c Mario.cpp -lSDL -lSDL_gfx -lSDL_image
clean:
rm -rf *.o
some parts of error(after typing make on terminal)
g++ shower.cpp -c -lSDL -lSDL_gfx -lSDL_image
g++ map.cpp -c -lSDL -lSDL_gfx
g++ -c Mario.cpp -lSDL -lSDL_gfx -lSDL_image
g++ main.cpp shower.o map.o Mario.o -lSDL -lSDL_gfx -lSDL_image -o mario
In file included from mario.h:11:0,
from main.cpp:7:
global_functions.cpp: In function ‘SDL_Surface* load_image(std::string)’:
global_functions.cpp:8:14: error: redefinition of ‘SDL_Surface* load_image(std::string)’
SDL_Surface *load_image( std::string filename )
^
In file included from shower.h:10:0,
from main.cpp:2:
global_functions.cpp:8:14: error: ‘SDL_Surface* load_image(std::string)’ previously defined here
SDL_Surface *load_image( std::string filename )
^