Jump to content
  • Advertisement
Sign in to follow this  
Juanu

A question about classes

This topic is 4808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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_H

class Sprite {
...
};

#endif //ndef IG_SPRITE_H

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]


????

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!