Error with constructors(with params) of class objects inside other class

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Error with constructors(with params) of class objects inside other class

 
0
  #1
Mar 24th, 2009
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)

  1. ...
  2. class Game{
  3.  
  4. private:
  5. Player P1;
  6. Player P2;
  7.  
  8.  
  9. public:
  10. Game(void);//constructor
  11. ...
  1. class Player{
  2. private:
  3. int x;//player x/y position
  4. int y;
  5.  
  6.  
  7. public:
  8. Player(int _x, int _y);//constructor
  9. ~Player(void);//destructor
  10.  
  11.  
  12. };
  13. ...
  14. Player::Player(int _x, int _y){
  15.  
  16. x = _x;
  17. y = _y;
  18.  
  19. }
**EDIT**
Also, if I do that:
  1. class Game{
  2.  
  3. private:
  4. Player P1(10, 210);
  5. Player P2(310, 210);
  6. ...
I get "C2059: syntax error : 'constant' "
Last edited by Icebone1000; Mar 24th, 2009 at 4:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 311
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: Error with constructors(with params) of class objects inside other class

 
0
  #2
Mar 24th, 2009
  1.  
  2. #include "stdafx.h"
  3.  
  4. class Player{
  5. private:
  6. int x;//player x/y position
  7. int y;
  8.  
  9.  
  10. public:
  11. Player(int _x, int _y);//constructor
  12. ~Player(void);//destructor
  13.  
  14.  
  15. };
  16.  
  17.  
  18. Player::Player(int _x, int _y){
  19.  
  20. x = _x;
  21. y = _y;
  22.  
  23. }
  24. int main(int argc, char* argv[])
  25. {
  26. printf("Hello World!\n");
  27.  
  28. Player p1(10,20);
  29. Player p2(310,210);
  30.  
  31. return 0;
  32. }
I just try this sample program and it compiles without a problem.
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 !
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Re: Error with constructors(with params) of class objects inside other class

 
0
  #3
Mar 24th, 2009
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
Last edited by Icebone1000; Mar 24th, 2009 at 4:59 pm.
Attached Files
File Type: zip codeIceBone.zip (2.2 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 311
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: Error with constructors(with params) of class objects inside other class

 
0
  #4
Mar 24th, 2009
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
  1. 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 !
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 311
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: Error with constructors(with params) of class objects inside other class

 
0
  #5
Mar 24th, 2009
It was compiling OK until I put the params on the Player constructor..
If you overload the default constructor then the compiler no longer put a default constructor ( this is not just a rule there is a reason for this think why ? )
So you can't say
  1. Player p ;
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 311
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: Error with constructors(with params) of class objects inside other class

 
0
  #6
Mar 25th, 2009
IDEA:
if you want to write
[code]
Player p ;
[code]
like that you should define the default values for your overloaded constructor .Like this

  1. class Player{
  2. private:
  3. int x;//player x/y position
  4. int y;
  5.  
  6.  
  7. public:
  8. Player(int _x=0, int _y=0);//constructor
  9. ~Player(void);//destructor
  10.  
  11.  
  12. };
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC