943,882 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 603
  • C++ RSS
Mar 24th, 2009
0

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

Expand Post »
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)

C++ Syntax (Toggle Plain Text)
  1. ...
  2. class Game{
  3.  
  4. private:
  5. Player P1;
  6. Player P2;
  7.  
  8.  
  9. public:
  10. Game(void);//constructor
  11. ...
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Mar 24th, 2009
0

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 24th, 2009
0

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

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
Attached Files
File Type: zip codeIceBone.zip (2.2 KB, 20 views)
Last edited by Icebone1000; Mar 24th, 2009 at 4:59 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Mar 24th, 2009
0

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

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
C++ Syntax (Toggle Plain Text)
  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 ? ).
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 24th, 2009
0

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

Quote ...
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
C++ Syntax (Toggle Plain Text)
  1. Player p ;
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 25th, 2009
0

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

IDEA:
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)
  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. };
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Sudoku Mini grid help!
Next Thread in C++ Forum Timeline: help!need making an random animation inside a function for allegro





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC