• Advertisement
Sign in to follow this  

How can a pass a class to a function?

This topic is 4430 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

I need to pass a class through a function. I keep getting an error C2143: Here is my code. /*****************************************/ /*Name:DetectCollision /*Author: Misael Aponte /*Date: 2006/01/02 /*Description:Detects collisions between two sprites /*****************************************/ int cCollison::DetectCollisionVer1(const cSprite &p_sprite1, const cSprite &p_sprite2) can anyone help me?

Share this post


Link to post
Share on other sites
Advertisement
Has cSprite been defined?
Is this the function definition?, if so there should be a ';' at the end.

Share this post


Link to post
Share on other sites
Yes I did define cSprite here is the code:
/********************************************************************
created: 2006/01/02
created: 2:1:2006 11:07
filename: c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cSprite.h
file path: c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo
file base: cSprite
file ext: h
author: Administrator

purpose: The main sprite engine
*********************************************************************/

#ifndef _CSPRITE_H_
#define _CSPRITE_H_

/*Include Files*/
#include "global.h"

/*****************************************/
/*Name:cSprite
/*Author: Misael Aponte
/*Date: 2005/12/30
/*Description:Handles the sprite functions
/*****************************************/
class cSprite : public cBitmap
{
public:
cSprite(void);
~cSprite(void);

//methods
void LoadAnimation( int *p_anim , int p_size, int p_columns, BITMAP *p_buffer ); //Loads the array for the animation
void PlayAnimation( int p_speed , BITMAP *p_buffer); //Plays the animation loop
void DisplayFrame(int p_frame);//displays a single frame
void DrawSprite(BITMAP *p_buffer, int p_Xpos, int p_Ypos);//draws the sprite onto the screen
int CurrentFrame(void);//Returns the current frame of the sprite
//Get the number of columns
int GetColumns(void);//Returns the amount of columns
void LoadAnimFromText(const char* p_filename, int p_columns);


private:
//variables
int m_currentframe; //The current frame
int m_maxframe; //Maximum amount of frames
int m_framedelay; //The delay for the animation loop
cArray<int> m_frames;//Dynamic array that holds animation cell from data
int m_counter; //Used to time the frames
int m_framecounter; //counts the frames
int m_sequence; //The current animation sequence
int m_Xframe; //The position of the x cell
int m_Yframe; //The position of the y cell
int m_columns;//The number of columns in the bitmap
int m_Xcellstartpos;//The cell X start position on the bitmap
int m_Ycellstartpos;//The cell Y

};

#endif //end of file

Share this post


Link to post
Share on other sites
Check the code right before this line too, make sure it ends appropriatly with a semicolon, ";", especially if there's something included right before it or something.

Share this post


Link to post
Share on other sites
These are the following errors
Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(21) : error C2819: type 'cSprite' does not have an overloaded member 'operator ->'
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cSprite.h(25) : see declaration of 'cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(21) : error C2227: left of '->GetXpos' must point to class/struct/union
type is 'const cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(22) : error C2819: type 'cSprite' does not have an overloaded member 'operator ->'
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cSprite.h(25) : see declaration of 'cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(22) : error C2227: left of '->GetXpos' must point to class/struct/union
type is 'const cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(23) : error C2819: type 'cSprite' does not have an overloaded member 'operator ->'
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cSprite.h(25) : see declaration of 'cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(23) : error C2227: left of '->GetXpos' must point to class/struct/union
type is 'const cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(23) : error C2819: type 'cSprite' does not have an overloaded member 'operator ->'
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cSprite.h(25) : see declaration of 'cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(23) : error C2227: left of '->GetWidth' must point to class/struct/union
type is 'const cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(24) : error C2819: type 'cSprite' does not have an overloaded member 'operator ->'
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cSprite.h(25) : see declaration of 'cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(24) : error C2227: left of '->GetXpos' must point to class/struct/union
type is 'const cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(24) : error C2819: type 'cSprite' does not have an overloaded member 'operator ->'
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cSprite.h(25) : see declaration of 'cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(24) : error C2227: left of '->GetWidth' must point to class/struct/union
type is 'const cSprite'
did you intend to use '.' instead?
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(25) : error C2662: 'cBitmap::GetYpos' : cannot convert 'this' pointer from 'const cSprite' to 'cBitmap &'
Conversion loses qualifiers
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(26) : error C2662: 'cBitmap::GetYpos' : cannot convert 'this' pointer from 'const cSprite' to 'cBitmap &'
Conversion loses qualifiers
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(27) : error C2662: 'cBitmap::GetYpos' : cannot convert 'this' pointer from 'const cSprite' to 'cBitmap &'
Conversion loses qualifiers
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(27) : error C2662: 'cBitmap::GetHeight' : cannot convert 'this' pointer from 'const cSprite' to 'cBitmap &'
Conversion loses qualifiers
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(28) : error C2662: 'cBitmap::GetYpos' : cannot convert 'this' pointer from 'const cSprite' to 'cBitmap &'
Conversion loses qualifiers
c:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(28) : error C2662: 'cBitmap::GetHeight' : cannot convert 'this' pointer from 'const cSprite' to 'cBitmap &'
Conversion loses qualifiers

Share this post


Link to post
Share on other sites
Quote:
Original post by boolai
These are the following errors
Settings\Administrator\My Documents\Visual Studio Projects\leo\cCollison.cpp(21) : error C2819: type 'cSprite' does not have an overloaded member 'operator ->'


If you are passing class as reference use (.) instead of (->)

Share this post


Link to post
Share on other sites
A lot of those errors are because you are accessing the class with -> when you should be using a .
You would use -> if it was a pointer that you were accessing, so you are passing by reference, but you aren't passing it as a pointer.

Other errors seem to be because you are trying to modify the classes in that function but since you are passing it by 'const', you shouldn't be modifying it.
If you are calling the get() functions to access private data, those get() functions should be const function.

Something like
int GetHeight() const
{
return height;
}

Share this post


Link to post
Share on other sites
For the first few errors, I bet that you are trying to access the members of the object with the -> operator rather than the . operator. Remember, you only use the -> operator on pointers (well, and objects which have that operator overloaded).

Share this post


Link to post
Share on other sites
Quote:
[...]\cCollison.cpp(21) : error C2819: type 'cSprite' does not have an overloaded member 'operator ->'

[...]

did you intend to use '.' instead?

Looks like you're trying to use the wrong member operator. Post your definition for cCollison::DetectCollisionVer1(...).

edit: too slow

Share this post


Link to post
Share on other sites
Yes I did make a mistake and used -> instead of a ".". But now I got an error C2061: So what is the best way to pass a class to a function?

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Make sure your collisions.h file includes your csprite.h file or else it won't know what cSprite class is.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

  • Advertisement