problems with my code, not sure why
hi, im programming an app in C++. i have a header file that has a 'player' class, but when i try to modify the varuiables of it i get some errors. here its the code:
player class.h:
class player{
public:
float size; //the size of the player
float x;
float y;
float z;
int score;
int lives;
float time;
};
main.cpp: (extract)
player me;
me.size=2; //line 37 here
me.y=5; //line 38
errors:
Error 2 error C2143: syntax error : missing ';' before '.' y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 37
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 37
Error 4 error C2371: 'me' : redefinition; different basic types y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 37
get the same 3 errors for line 38. cheers for any help :)
Re: problems with my code, not sure why
Did you
Code:
#include "player.h"
in main.cpp?
Hope that helps.
Re: problems with my code, not sure why
yeh, i did #include the right file, i also added it into the project through the 'add existing file' thing. its starting to annoy me :(
Re: problems with my code, not sure why
Try posting the whole project here, there must be something wrong with your .h/.cpp file.
Re: problems with my code, not sure why
il post it tomorrow... the file is on my account in uni, and im on my house comp at the mo. cheers
Re: problems with my code, not sure why
ok, heres my code:
____________________________________________
player class.h:
class player{
public:
float size; //the size of the player
float x;
float y;
float z;
int score;
int lives;
float time;
};
_____________________________________________________
main.cpp (only down to the error, as the code is very long):
#include <windows.h>
#include <stdio.h>
#include <mmsystem.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include "tex.h"
#include "MilkshapeModel.h"
#include "player calss.h"
#define COLOUR_DEPTH 16 //Colour depth
typedef struct Mouse
{
int x,y;
}Mouse;
float speedx=0, speedz=0;
float tiltx=0, tilty=0;
float itsx=0, itsy=5, itsz=0;
float rot=0.0;
float rotate=0;
float ySpeed=0;
float yAccel=-0.1;
int falling;
int Walls[1][5];
int ground[2][9];
int collision=false;
Model *robo = NULL;
player me;
me.size=2; ///////////////////////////////////error occurs here
me.y=5;
______________________________________________________
if you havent guessed, im trying to program in game in openGL. i am just using the class to tidy the code up a bit. i havent included the whole main.cpp file as it is about 1000 lines long, and i doubt any of it is causing the problem... or could it?
[edit] also, the filename is actually "player calss.h" because i misstyped it when i saved it, so just included as is.
Re: problems with my code, not sure why
Reading error messages, it seems for me that name me is already defined, probably as a type name in one of header files. Try different name for your object and check if error still occurs.
Cheers,
Hob
Re: problems with my code, not sure why
I'm not 100% sure about this because I've never seen it, but your player class does not have a constructor! (This could be your problem.)
Normally you'll have in the header:
Code:
player class.h:
class player{
public:
player();
~player();
float size; //the size of the player
float x;
float y;
float z;
int score;
int lives;
float time;
};
Along with that, you would have an implementation (or it could be included in the header if needed.
Code:
class player{
public:
player(){ };
~player(){ };
...
I suspect that the error really occurred on the line preceding the one you point to, which would indicate it could not create an object of the player class.
Hope that helps.
Re: problems with my code, not sure why
just tried changing it to 'murloc' and then 'randomenamething' and it still came up with the same error. do i need to #include any files in my header file?
Re: problems with my code, not sure why
i just updated the player class to:
class player{
public:
player(){};
~player(){};
float size; //the size of the player
float x;
float y;
float z;
int score;
int lives;
float time;
};
the error still occurs. is that the right syntax for just keeping it in the class?
Re: problems with my code, not sure why
hi,
just chk ur spelling...u typed
#include "player calss.h"
instead of "player class.h"
this shud solve ur problem i guess..
Re: problems with my code, not sure why
Quote:
Originally Posted by kaira
hi,
just chk ur spelling...u typed
#include "player calss.h"
instead of "player class.h"
this shud solve ur problem i guess..
You need a spell checker. This is a professional programming site, not a teen chat room. Please use real words. Not only does it reflect better on you, but it helps those for whom English isn't their first language.
Re: problems with my code, not sure why
Quote:
Originally Posted by weewun
main.cpp: (extract)
player me;
me.size=2; //line 37 here
me.y=5; //line 38
errors:
Error 2 error C2143: syntax error : missing ';' before '.' y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 37
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 37
Error 4 error C2371: 'me' : redefinition; different basic types y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 37
get the same 3 errors for line 38. cheers for any help :)
Since I don't see an entire file (I understand, 1000 lines and everything...), I can only guess.
Your lines 37 and 38 are OUTSIDE any function body? Then compiler assumes (and it says so in the error message) that you define another variable called "me", of a type "int" (by default) and are trying to use "." on it!
Re: problems with my code, not sure why
Why dont you attach actual code files ?
Re: problems with my code, not sure why
Quote:
Originally Posted by kaira
just chk ur spelling...u typed
#include "player calss.h"
instead of "player class.h"
this shud solve ur problem i guess..
This would have produced two different errors: missing include file (some compilers would stop there) and unknown type "Player" on line 36.