Initializing objects inside of constructors C++

Advertisements

I am new to C++ and cannot figure out this error I am getting. I have created a Card class that stores a suit and rank as an unsigned integer. I also have a Maingame class that runs multiple functions. I want to create 5 Card objects inside of my Maingame class, but I am having a problem initializing them. My Card class is as such..

class Card
{
public:
    Card(unsigned int s, unsigned int r);
private:
    unsigned int _suit;
    unsigned int _rank;
};

And the constructor for Card()

Card::Card(unsigned int s, unsigned int r) {
    _suit = s;
    _rank = r;
}

Creating a Card object in the Maingame class

class Maingame
{
public:
    Maingame();
    void run();
    void out();
private:
    Card card1;
};

And the constructor for Maingame

Maingame::Maingame() {
    card1(DIAMONDS, THREE);
}

This is where I get an error
"call of an object of a class type without appropriate operator() or conversion to pointer-to-function type"

>Solution :

Your syntax is wrong. It seems you are attempting an initializer list, this is the syntax

Maingame::Maingame() : card1(DIAMONDS, THREE)
{
}

If you had more than one item to initialise then you would separate them with commas.

Leave a ReplyCancel reply