Kiprin Kiprin - 4 months ago 32
C++ Question

Can't using constructor c++

I Have constructor for the class Game, which define a Deck.

Game::Game(char*configurationFile):turn(0), playingPlayer(0)
vector<Card*> vec;
initialState ="";


Deck deck;

and the constructor of Deck:

Deck::Deck(vector <Card*> vec):deck(vec), NumOfCard(vec.size()){}

of course its only some parts of the code, but I keep getting the error:

no match for call to ‘(Deck) (std::vector&)’

on line :

and I cant understand why. Thanks all for helping!


Lets make some order here. In the Game.h file you'd want to declare all you class members and class functions (in some cases, you can even provide implementation to your functions in the header, or write them as inline, but that's not the case) So Game.h:

   vector<Player *> players;  //The list of the players
   Deck deck;
   string initialState; int turn; int playingPlayer;

Notice that you don't need to create local object, since you probably dont need that vector after the constructor finishes. Also, if you have a subobject as a class member, unless you initialize it in the init-list, it will be constructed with the default constructor, hence the call in your original code invokes the copy assignment operator. You can see this in this simple code:

You can indeed implement the constructor in the CPP file, hence Game.cpp:

Game::Game(char *configurationFile):turn(0), playingPlayer(0),initiateState(""), deck(vector<Card *>()){}

Now, for the construction of deck you'd probably want:

Deck::Deck(const vector<Card *>&vec){ //implement according to your class}

Notice that in your code, you made a really weird call in deck(vec), perhaps you meant Deck(vec), which also doesn't make sense since it a recursive call to the constructor itself.

Hope that helped a bit.