| | |
Error with constructors(with params) of class objects inside other class
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
How I can do it?
I want a constructor of a object to have parameters, this object is inside other class..the compiler say the class dont have proper default constructors..(error C2512)
**EDIT**
Also, if I do that:
I get "C2059: syntax error : 'constant' "
I want a constructor of a object to have parameters, this object is inside other class..the compiler say the class dont have proper default constructors..(error C2512)
C++ Syntax (Toggle Plain Text)
... class Game{ private: Player P1; Player P2; public: Game(void);//constructor ...
C++ Syntax (Toggle Plain Text)
class Player{ private: int x;//player x/y position int y; public: Player(int _x, int _y);//constructor ~Player(void);//destructor }; ... Player::Player(int _x, int _y){ x = _x; y = _y; }
Also, if I do that:
C++ Syntax (Toggle Plain Text)
class Game{ private: Player P1(10, 210); Player P2(310, 210); ...
Last edited by Icebone1000; Mar 24th, 2009 at 4:30 pm.
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" class Player{ private: int x;//player x/y position int y; public: Player(int _x, int _y);//constructor ~Player(void);//destructor }; Player::Player(int _x, int _y){ x = _x; y = _y; } int main(int argc, char* argv[]) { printf("Hello World!\n"); Player p1(10,20); Player p2(310,210); return 0; }
and I just google your error. Probally you are not the only one who
get a error like this. well first search is MSDN page that contain details about this error.
it says torken error.
so sounds like it's little bit complicated to see this without looking at other source codes and dependencies.
anyway a one pice of advice , to avoid unnecessary problems always use a standard. Don't use _ starting variables. They are reserved to use internally inside the C++ compiler itself. Try it again with removing _ starting variable names.
If you need to learn about how to name your variables properly google "coding standards C++"
* you can use _ characters on variables but don't start with it.
Last edited by NicAx64; Mar 24th, 2009 at 4:49 pm. Reason: I have mentioned to remove _ contain variable names , but it should be corrected to remove _ starting variable names
Nothing like a kernel pannic !
But my Player object is inside other class ( I though that was the problem) I also using .h and .cpp files to each class...
Im attaching my files to .rar file..
It was compiling OK until I put the params on the Player constructor..
Thanks for the tips
Im attaching my files to .rar file..
It was compiling OK until I put the params on the Player constructor..
Thanks for the tips
Last edited by Icebone1000; Mar 24th, 2009 at 4:59 pm.
and for your first question (C2512) , just google that error and
you will find the answer. I can't post duplicate here. It's violation of member rules.
the problem is think that , if you call
inside main , it will looking for the default constructor and calls it.
when you overload it there is no default constructor ( there is a reason for this rule think why ? ).
you will find the answer. I can't post duplicate here. It's violation of member rules.
the problem is think that , if you call
C++ Syntax (Toggle Plain Text)
Player p;
inside main , it will looking for the default constructor and calls it.
when you overload it there is no default constructor ( there is a reason for this rule think why ? ).
Nothing like a kernel pannic !
•
•
•
•
It was compiling OK until I put the params on the Player constructor..
So you can't say
C++ Syntax (Toggle Plain Text)
Player p ;
Nothing like a kernel pannic !
IDEA:
if you want to write
[code]
Player p ;
[code]
like that you should define the default values for your overloaded constructor .Like this
if you want to write
[code]
Player p ;
[code]
like that you should define the default values for your overloaded constructor .Like this
C++ Syntax (Toggle Plain Text)
class Player{ private: int x;//player x/y position int y; public: Player(int _x=0, int _y=0);//constructor ~Player(void);//destructor };
Nothing like a kernel pannic !
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Sudoku Mini grid help!
- Next Thread: help!need making an random animation inside a function for allegro
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





