HoKy22
HoKy22

Reputation: 4217

C++ No instance of constructor "Card::Card" matches the argument list

This is my Card struct header file:

#include "stdafx.h"
enum Suits {clubs, diamonds, hearts, spades};
enum Ranks {two = 2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};

struct Card {   
    Card (Suits suit, Ranks rank);
private:
    Suits suit_;
    Ranks rank_;
};

I initialize the Card member variables in my cpp:

#include "stdafx.h"    
#include "Card.h"
#include "Header.h"

using namespace std;

Card::Card (Suits suit, Ranks rank) : suit_(suit), rank_(rank) {}

Now, I am trying to parse a bunch of card definition string, such as 2C, 3h, 7s, 10h in a function

int FileParsing(vector<Card> & v, char * FileName) {
    ... //omiting the details, basically open FileName, parse card definition strings
    //After I finish parsing 10h, I tried to push it back
    v.push_back(Card(ten, hearts)); //got an error here
    ... 
    return 0;
}

My suspection is the type conflicts in Card(Suits, ranks), but I am not sure. Any input will be greatly appreciated!!!

Upvotes: 0

Views: 2209

Answers (4)

JKor
JKor

Reputation: 3842

If ten and hearts are of type Suits and Ranks accordingly, then it looks like you just have your constructor arguments in the wrong order. The constructor is Card(Suits, Ranks), but you are trying to call Card(Ranks,Suits). Switch around your constructor arguments and it should work.

Upvotes: 1

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361692

v.push_back(Card(ten, hearts)); //got an error here

should be written as:

v.push_back(Card(hearts, ten)); //fixed

Because the first argument to Card should be Suits, and second should be Ranks.

Upvotes: 1

TheBuzzSaw
TheBuzzSaw

Reputation: 8836

Your constructor takes suit then rank.

Card::Card (Suits suit, Ranks rank) : suit_(suit), rank_(rank) {}

Your parameters are backwards when you create it here.

v.push_back(Card(hearts, ten));

Upvotes: 1

Leo Chapiro
Leo Chapiro

Reputation: 13979

v.push_back(Card(ten, hearts)); //got an error here 

must this not be

v.push_back(Card(hearts, ten));

?

Upvotes: 3

Related Questions