How can a pass a class to a function?

Started by
16 comments, last by Oluseyi 18 years, 3 months ago
Quote:Original post by boolai
So what is the best way to pass a class to a function?

Depends. For your function, constant references will be fine. Post your function definition (or the calling line, if that's where c2061 occurs).
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Advertisement
Ok here is my function. I am trying to do the collsion detection from one of the articles. I am trying to learn to so I can modify it.

#ifndef _CCOLLISON_H_
#define _CCOLLISON_H_

/*Includes*/
#include "global.h"


class cCollison
{
public:
cCollison(void);
~cCollison(void);

//Functions
int DetectCollisionVer1(cSprite &p_sprite1,cSprite &p_sprite2);

private:
int m_left1;
int m_left2;
int m_right1;
int m_right2;
int m_top1;
int m_top2;
int m_bottom1;
int m_bottom2;

};
#endif //end of file

here is the defination
int cCollison::DetectCollisionVer1(cSprite &p_sprite1, cSprite &p_sprite2)
{
//Set the attributes //meaning were are going to record the object stats
//sprite1 first then sprite2
m_left1 = p_sprite1.GetXpos();
m_left2 = p_sprite2.GetXpos();
m_right1 = p_sprite1.GetXpos() + p_sprite1.GetWidth();
m_right2 = p_sprite2.GetXpos() + p_sprite2.GetWidth();
m_top1 = p_sprite1.GetYpos();
m_top2 = p_sprite2.GetYpos();
m_bottom1 = p_sprite1.GetYpos() + p_sprite1.GetHeight();
m_right2 = p_sprite2.GetYpos() + p_sprite2.GetHeight();

//The following is not a collision so disregard
if (m_bottom1 < m_top2) return(0);
if (m_top1 > m_bottom2) return(0);

if (m_right1 < m_left2) return(0);
if (m_left1 > m_right2) return(0);


return (1);
}//end of function

the Error appears here: error C2061: syntax error : identifier 'cSprite'
appears at the declaration of the DetectCollisionVer1() method
Just trying this learn this very challenging subject of game programming.
Make sure your collisions.h file includes your csprite.h file or else it won't know what cSprite class is.
Yes I have it in the header file called global.h
Just trying this learn this very challenging subject of game programming.
In the code you posted above, csprite.h includes global.h. You'll need to include csprite.h in ccollision.h in order to reference your cSprite class.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Quote:Original post by stylin
In the code you posted above, csprite.h includes global.h. You'll need to include csprite.h in ccollision.h in order to reference your cSprite class.


That is what I said, which is why he responded that in global.h he has #include "csprite.h".

I'm stumped why it doesn't work. I debug better if I have the files [grin]. It is easier to check possible solutions or various typos.
BTW you don't pass classes but objects created by classes.

(And in this case: a reference to a cSprite object which cannot be changed by the function).
Quote:Original post by nprz
Quote:Original post by stylin
In the code you posted above, csprite.h includes global.h. You'll need to include csprite.h in ccollision.h in order to reference your cSprite class.


That is what I said, which is why he responded that in global.h he has #include "csprite.h".

Except that he's wrong. csprite.h includes global.h, and collision.h includes global.h. collision.h is completely ignorant of csprite.h.

That would be why "it doesn't work."

@boolai:
Read this: Organizing Code Files in C and C++.

This topic is closed to new replies.

Advertisement