Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

witty

class problems

This topic is 5662 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 am trying to do 2 functions to modify variables in a class. They won''t compile. There are 2 ints, x and y. I am writing 1 function to return these values and 1 to set them: Here''s the functions:
int playerclass::get_position(p,q) 
   int *p, *q; {
   *p=X;
   *q=Y;
   return 0; }
 
/* for setting the player position at start of level.*/
int playerclass::set_position(p,q) 
   int q, p; {
   X=p;
   Y=q;
   return 0; } 
Here''s the class:
class playerclass
{ 
public:
int init();
int update_player();
/*int draw();*/
int get_position(p,q);
int set_position(p,q); 
private:
int centerx, centery;
int X, Y;
float dX, dY;
int health;
int mass;
}; 
and here''s the error I get.
main.cpp:82: `p'' was not declared in this scope
main.cpp:82: `q'' was not declared in this scope
main.cpp:82: invalid data member initialization
main.cpp:82: (use `='' to initialize static data members)
main.cpp:83: `p'' was not declared in this scope
main.cpp:83: `q'' was not declared in this scope
main.cpp:83: invalid data member initialization
main.cpp: In member function `int playerclass::update_player()'':
main.cpp:109: warning: assignment to `int'' from `float''
main.cpp:109: warning: argument to `int'' from `float''
main.cpp:110: warning: assignment to `int'' from `float''
main.cpp:110: warning: argument to `int'' from `float''
main.cpp:114: warning: assignment to `int'' from `double''
main.cpp:114: warning: argument to `int'' from `double''
main.cpp: At global scope:
main.cpp:120: `p'' was not declared in this scope
main.cpp:120: `q'' was not declared in this scope
main.cpp:121: `int playerclass::get_position'' is not a static member of `class 
   playerclass''
main.cpp:121: initializer list being treated as compound expression
main.cpp:121: syntax error before `int''
main.cpp:123: ISO C++ forbids declaration of `q'' with no type
main.cpp:123: `Y'' was not declared in this scope
main.cpp:124: syntax error before `return''
main.cpp:127: `p'' was not declared in this scope
main.cpp:128: `int playerclass::set_position'' is not a static member of `class 
   playerclass''
main.cpp:128: initializer list being treated as compound expression
main.cpp:128: syntax error before `int''
main.cpp:130: ISO C++ forbids declaration of `Y'' with no type
main.cpp:130: invalid conversion from `int*'' to `int''
main.cpp:131: syntax error before `return''
 
What does this mean? Please help. (I know what the things about floats and ints are, thats fine - not a problem)

Share this post


Link to post
Share on other sites
Advertisement
You need to assign a type to your parameters p and q, ie int p, float q, or whatever you need them to be

Share this post


Link to post
Share on other sites
this is how your functions should be

void playerclass::get_position(int &p, int &q) const
{
p=X;
q=Y;
}

void playerclass::set_position(const int p, const int q)
{
X=p;
Y=q;
}

Share this post


Link to post
Share on other sites
And also add the type into the class definition too


class playerclass{ public:
int init();
int update_player();
/*int draw();*/
int get_position(INT p, INT q);
int set_position(INT p, INT q);
...

Share this post


Link to post
Share on other sites
I've already done all that. It just came up with different errors, but saying int,int instead of p,q.
I've got books on C++, thats half the reason I'm doing this stuff. In reality I would just use public variables, but I'm trying to teach myself the ins and outs.
Sorry if you think I've been wasting your time.
[edit] Thanks, changing it to void worked a treat. Cheers!

[edited by - witty on June 16, 2003 5:52:19 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!