Multiple definiton of a method

I got 2 class defined in a .h file, but in the .cpp file, the compiler is throwing a error, it say that I got a multiples definitions of the same method, why is happening this? I can't find the answer
For example it can be due to the first definition of a method inside the class and the second definition of the same method outside the class.
but I have not defined anything twice, you could see

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
40
41
42
43
44
45
#ifndef CSPRITE_H_
#define CSPRITE_H_

#define TRUE 1
#define FALSE 0

// CFrame representa un frame independiente de un sprite.
class CFrame {
public:
	SDL_Surface *img;
	void load(char *path);
	void unload();
};


// La clase CSprite está formada por un array de frames;
class CSprite {
private:
	int posx,posy;
	int estado;
	int nframes;
	int cont;

public:
	CFrame *sprite;
	CSprite(int nf);
	CSprite();
	void addframe(CFrame frame);
	void selframe(int nf);
	int frames() {return nframes-1;}
	void setx(int x) {posx=x;}
	void sety(int y) {posy=y;}
	void addx(int c) {posx+=c;}
	void addy(int c) {posy+=c;}
	int getx(); //{return posx;}
	int gety(); //{return posy;}
	int getw(); //{return sprite[estado].img->w;}
	int geth(); //{return sprite[estado].img->h;}
	void draw(SDL_Surface *superficie);
	int colision(CSprite sp);
};

#endif /* CSPRITE_H_ */



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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90


#include <SDL/SDL.h>
#include "csprite.h"

// Sprite Class implementation

void CFrame::load(char *path) {
	img=SDL_LoadBMP(path);

	// Asignamos el color transparente al color rojo.
	SDL_SetColorKey(img,SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(img->format,255,0,0));
	img=SDL_DisplayFormat(img);

}

void CFrame::unload(){
	SDL_FreeSurface(img);
}



CSprite::CSprite(int nf) {
	sprite=new CFrame[nf];
	nframes=nf;
	cont=0;
}


CSprite::CSprite() {
int	nf=1;
	sprite=new CFrame[nf];
	nframes=nf;
	cont=0;
}


void CSprite::addframe(CFrame frame) {
	if (cont<nframes) {
		sprite[cont]=frame;
		cont++;
	}
}



void CSprite::selframe(int nf) {
	if (nf<=nframes) {
		estado=nf;
	}
}

int CSprite::getx() {return posx;}

int CSprite::gety() {return posy;}

int CSprite::getw() {return sprite[estado].img->w;}
int CSprite::geth() {return sprite[estado].img->h;}


void CSprite::draw(SDL_Surface *superficie) {
	SDL_Rect dest;

	dest.x=posx;
	dest.y=posy;
	SDL_BlitSurface(sprite[estado].img,NULL,superficie,&dest);
}



int CSprite::colision(CSprite sp) {
int w1,h1,w2,h2,x1,y1,x2,y2;

	w1=getw();			// ancho del sprite1
	h1=geth();			// altura del sprite1
	w2=sp.getw();		// ancho del sprite2
	h2=sp.geth();		// alto del sprite2
	x1=getx();			// pos. X del sprite1
	y1=gety();			// pos. Y del sprite1
	x2=sp.getx();		// pos. X del sprite2
	y2=sp.gety();		// pos. Y del sprite2

	if (((x1+w1)>x2)&&((y1+h1)>y2)&&((x2+w2)>x1)&&((y2+h2)>y1)) {
		return TRUE;
	} else {
		return FALSE;
	}
}



if I would put include "csprites.h" the erros will disappear, but a new error came here, and will tell: error expeted ";" before "*" token, on the SDL_Surface *img; (line 10 of CFrame class)

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
#include "csprite.cpp"

#ifndef PERSONAJE_H_
#define PERSONAJE_H_

class Personaje
{
  private:
    int vidas;//inicializar en 1
    int speed;//en milisegundos
    bool wall_Pass,bomb_Pass,bomb_Bam;
    CSprite imagen; //posible arreglo

  public:
    void setVidas(int var);
    void setSpeed(int var);
    void setWallPass(bool var);
    void setBombPass(bool var);
    void setBombBam (bool var);
    int  getVidas()const;
    int  getSpeed()const;
    bool getWallPass()const;
    bool getBombPass()const;
    bool getBombBam()const;
};

#endif // PERSONAJE_H_
I did run this code...Maybee..... I just put SDL/SDL.h on csprite.h code, in personaje.h I put csprite.h and all work.... =) xD

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
40
41
42
43
44
45

#include <SDL/SDL.h>
#ifndef CSPRITE_H_
#define CSPRITE_H_

#define TRUE 1
#define FALSE 0

// CFrame representa un frame independiente de un sprite.
class CFrame {
public:
	SDL_Surface *img;
	void load(char *path);
	void unload();
};


// La clase CSprite está formada por un array de frames;
class CSprite {
private:
	int posx,posy;
	int estado;
	int nframes;
	int cont;

public:
	CFrame *sprite;
	CSprite(int nf);
	CSprite();
	void addframe(CFrame frame);
	void selframe(int nf);
	int frames() {return nframes-1;}
	void setx(int x) {posx=x;}
	void sety(int y) {posy=y;}
	void addx(int c) {posx+=c;}
	void addy(int c) {posy+=c;}
	int getx(); //{return posx;}
	int gety(); //{return posy;}
	int getw(); //{return sprite[estado].img->w;}
	int geth(); //{return sprite[estado].img->h;}
	void draw(SDL_Surface *superficie);
	int colision(CSprite sp);
};

#endif /* CSPRITE_H_ */ 

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

#include "csprite.h"

// Sprite Class implementation

void CFrame::load(char *path) {
	img=SDL_LoadBMP(path);

	// Asignamos el color transparente al color rojo.
	SDL_SetColorKey(img,SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(img->format,255,0,0));
	img=SDL_DisplayFormat(img);

}

void CFrame::unload(){
	SDL_FreeSurface(img);
}



CSprite::CSprite(int nf) {
	sprite=new CFrame[nf];
	nframes=nf;
	cont=0;
}


CSprite::CSprite() {
int	nf=1;
	sprite=new CFrame[nf];
	nframes=nf;
	cont=0;
}


void CSprite::addframe(CFrame frame) {
	if (cont<nframes) {
		sprite[cont]=frame;
		cont++;
	}
}



void CSprite::selframe(int nf) {
	if (nf<=nframes) {
		estado=nf;
	}
}

int CSprite::getx() {return posx;}

int CSprite::gety() {return posy;}

int CSprite::getw() {return sprite[estado].img->w;}
int CSprite::geth() {return sprite[estado].img->h;}


void CSprite::draw(SDL_Surface *superficie) {
	SDL_Rect dest;

	dest.x=posx;
	dest.y=posy;
	SDL_BlitSurface(sprite[estado].img,NULL,superficie,&dest);
}



int CSprite::colision(CSprite sp) {
int w1,h1,w2,h2,x1,y1,x2,y2;

	w1=getw();			// ancho del sprite1
	h1=geth();			// altura del sprite1
	w2=sp.getw();		// ancho del sprite2
	h2=sp.geth();		// alto del sprite2
	x1=getx();			// pos. X del sprite1
	y1=gety();			// pos. Y del sprite1
	x2=sp.getx();		// pos. X del sprite2
	y2=sp.gety();		// pos. Y del sprite2

	if (((x1+w1)>x2)&&((y1+h1)>y2)&&((x2+w2)>x1)&&((y2+h2)>y1)) {
		return TRUE;
	} else {
		return FALSE;
	}
}



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
#include "csprite.h"

#ifndef PERSONAJE_H_
#define PERSONAJE_H_

class Personaje
{
  private:
    int vidas;//inicializar en 1
    int speed;//en milisegundos
    bool wall_Pass,bomb_Pass,bomb_Bam;
    CSprite imagen; //posible arreglo

  public:
    void setVidas(int var);
    void setSpeed(int var);
    void setWallPass(bool var);
    void setBombPass(bool var);
    void setBombBam (bool var);
    int  getVidas()const;
    int  getSpeed()const;
    bool getWallPass()const;
    bool getBombPass()const;
    bool getBombBam()const;
};

#endif // PERSONAJE_H_ 
Last edited on
Topic archived. No new replies allowed.