Jump to content
  • Advertisement
Sign in to follow this  

How can a pass a class to a function?

This topic is 4522 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
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!