stupid class functions
 |
|
June 16th, 2003, 05:19 PM
|
#1
|
|
Ultimate Member
Join Date: Oct 2002
Location: Scotland, UK
Posts: 3,221
|
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:
Code:
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:
Code:
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.
Code:
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)
__________________
_____
NuKeS
|
|
|
June 17th, 2003, 07:15 AM
|
#2
|
|
Junior Member
Join Date: Jun 2003
Location: Reno
Posts: 20
|
Well heres a couple quick things I noticed in the code provided.
How about this:
Code:
class playerclass
{
public:
player (); // Default Constructor
int init();
int update_player();
/*int draw();*/
int get_position(int, int); // What are the prurposes of p and q?
/*int get_position(int*, int*); */ If you want them declared as pointers ( which I dont think you really need to)
int set_position(int ,int );
private:
int centerx, centery;
int X, Y; // What do X and Y represent?
float dX, dY;
int health;
int mass;
};
Code:
int playerclass::get_position( int* p, int* q )
{
*/int *p, *q; // Are these the same as the p and q passed in?
If they are they need to be defined as pointers
otherwise use different variables.*/
// What are you saying here?
p=&X; // p = "address of" X
q=&Y; // same
return 0; // You might want to return the actual postion rather than true or false
}
int playerclass::set_position( p, q )
{
X=p;
Y=q;
return 1; // Success?
}
I could have misinterpreted what you are trying to accomplish. I just picked out some syntactical errors I noticed.
My C is a little rusty (especially pointers  ) so I might not be totally 100% correct but it's a start. 
Like I said I dont exactly what you are trying to do with the code so maybe you could give a little more detail so we might be able to clean up all the errors.
I also had a project a couple semesters back where we had to write a program that would simulate a basketball, football, soccer or whatever we wanted. So I might be able to help if thats what your doing.
Hopefully this cleans it up a little and helps you out
-Late
|
|
|
June 17th, 2003, 05:18 PM
|
#3
|
|
Ultimate Member
Join Date: Oct 2002
Location: Scotland, UK
Posts: 3,221
|
Sorry, I solved it. It turns out I had to define p and q about 10 times in different places to get it to work, thanks for replying though.
When I finish the project, or make some serious headway, I'll post the code.
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|
Most Active Discussions
|
|
|
|
|
Recent Discussions 
|
|
|
|
|
|