Creating a class object inside another class's constuctor
I have a Hand class and a Player class. I want the player to own the hand so I am thinking I need a prototype in the Player.h and then in my Player.ccp constructor create it. This is not working. I keep getting an "incomplete type" error. Not sure how I should approach this issue. Am I defeating the purpose of OO by trying to do this way? Any help would be great, thanks.
My player class - Code:
myHand("deck"); Code:
Hand myHand; |
I couldn't compile your example program, maybe you left out important parts.
|
(so would be nice to give more details, probably some sources) Otherwise the problem is in line 34.
|
the compiler needs to see the whole Hand class where it needs to see it, this is where it needs to know the size,
if your Player.h declares a value object of type Hand, this is the point where the compiler need to know everything about Hand. so you it seems you need to include Hand.h in Player.h please have a look at http://www.linuxquestions.org/questi...gs-4175464257/ |
Or use pointers:
Code:
/* player.h */ |
The two choices for dealing with the incomplete type were given by az4 and NevemTeve.
But, seperate from those details, I can't see how your line of code could be correct: Code:
myHand("deck"); I would have expected something like: Code:
Player::Player( ... ) : Code:
Player::Player( ... ) : Code:
Player::Player( ... ) |
Off: the last point could be argued http://yosefk.com/c++fqa/ctors.html#fqa-10.6
|
I usually do post code but I was thinking my whole approach was wrong, not the code.
I tried something different this time. Like what a4z mentioned, I tried creating the Hand object within the Player class but now I am getting errors everywhere I use a method from the Hand class. Example - myHand.addCard(myDeck->dealOne()); Nevemteve, to use a pointer as you mentioned wouldn't I need to pass that in through the constructor? Code:
//Player.cpp Code:
#include <string> |
Be a good boy and create a small, complete, minimal example...
|
Be a good boy???
It comes down to just the first 13 lines of code in the player class. I posted all the other code because of recent comments. |
All I ask is a complete, minimal example that compiles in itself (or shows the compilation error you want to demonstrate).
|
Hand myHand is not in the player hpp
#include "Hand.h" needs to go into hpp you can not call a constructor on myHand when myHand is already initialized you need to make this in the construction itself search for class member initialization it looks like this Code:
Player::Player(Deck *deck) |
Quote:
Here: Code:
Hand myHand(string assocDeck); Here: Code:
Player::Player(Deck *deck){ You should be include <string> in your implementation file and you could read up on header guards. |
a4z - I tried what you suggested but the compiler kept squawking at me for adding Hand header to Player header. I received a error in Class.cpp. I did get it work and while I do no think it is a bad way of doing it I know I easily could be wrong in assuming that. Here is what I did. I added Hand *myHand to top of Player.cpp and in the constructor {...myHand = new Hand(deck->getName());...}
SoftSprocket - those illegal constructs were me frustrated and throwing stuff out there to see if it would stick. I know I know, not the way to go about it. |
Quote:
what you post is incomplete and not enough to fix the problem, put the stuff on github or somewhere else if you want to make it easy to help and you are not able to reduce the problem so that what you post is reproducible. |
All times are GMT -5. The time now is 02:34 PM. |