A question about classes

Started by
5 comments, last by Zahlman 18 years, 7 months ago
Hi.. I have a question I have a class that is handled on the main, and other class that is handled on bothj So I need to pass a pointer of the second class, to the first one. Is this ok? Ok. SO this is what I have the first class:
[SOURCE]
class ManejaSup
{
public:
	//Constructor
	ManejaSup( SDL_Surface* pantalla );

	//Destructor
	~ManejaSup();

	void DibujaSprite( Sprite* sprite );

};

[/SOURCE]
You can see that it takes a pointer to the Sprite class as a parameter. Heres is the Sprite Class:
[SOURCE]

class Sprite
{
public:
	//Constructor
	Sprite( int ANCHO, int ALTO, int DIST, char* ARCHIVO, int COLUMNAS, int FILAS );

	//Destructor
	~Sprite();

	SDL_Surface* imagen();

	enum Direccion { IZQUIERDA, DERECHA };

	Direccion dir;

	bool bMover;

private:

	SDL_Rect rctSprite;

	SDL_Surface* sprite;

	int Frame;
};

[/SOURCE]
So. in small words i need to call a function on One class that uses the parametrers of another clas.. Can iDo it? I did it and I got this output: c:\Juanu\Proyecto Juego\Engine_AnimaSprite\src\sprite.h(9): error C2011: 'Sprite' : 'class' type redefinition What could it be?? Thanx!
Advertisement
Quote:Original post by Juanu
c:\Juanu\Proyecto Juego\Engine_AnimaSprite\src\sprite.h(9): error C2011: 'Sprite' : 'class' type redefinition


What could it be??


Looks like you #included sprite.h multiple times within a translation unit (like, main.cpp #includes a.h and b.h, and they both #include sprite.h).

The way to fix this is with an "include guard". Try:

sprite.h :
#ifndef IG_SPRITE_H#define IG_SPRITE_Hclass Sprite {    ...};#endif //ndef IG_SPRITE_H

Yes, you can do it. That error says that you've already defined a class called "Sprite". Are you using proper inclusion guards for your header (#pragma once or #ifdef __HEADER_H__ etc)?

If you need to forward declare a class (E.g. if you want to not need to include your sprite header to use your ManejaSup class), you can write class Sprite;
That will allow you to use references and pointers to Sprite objects, but not deal with actual objects or access any member functions or member variables until you include your sprite header.
Ok
I used the
#ifndef SPRITE_H
#define SPRITE_H

#endif

And now im'm having this output

c:\Juanu\Proyecto Juego\Engine_AnimaSprite\src\manejasup.h(17): error C2061: syntax error : identifier 'Sprite'


And on Dbl Click points here:

[SOURCE]class ManejaSup{public:	ManejaSup( SDL_Surface* pantalla );	~ManejaSup();        //This is where it says the error is... ?????	void DibujaSprite( Sprite* sprite );};[/SOURCE]


????
Anyone? Please??
I'm Stuck in here!
Either include the header your sprite is in before you declare your ManejaSup class, or add the line class Sprite; before the line class ManejaSup.
*bUrp*

This topic is closed to new replies.

Advertisement