Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Initializing objects inside of constructors C++

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading