How can a pass a class to a function?

Started by
16 comments, last by Oluseyi 18 years, 3 months ago
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?
Just trying this learn this very challenging subject of game programming.
Advertisement
Has cSprite been defined?
Is this the function definition?, if so there should be a ';' at the end.
"Are you threatening me, Master Jedi?" - Chancellor Palpatine
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
Just trying this learn this very challenging subject of game programming.
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.
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
Just trying this learn this very challenging subject of game programming.
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 (->)

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;
}
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).
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
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
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?
Just trying this learn this very challenging subject of game programming.

This topic is closed to new replies.

Advertisement