C++ object not working with dot operator

Jake Gearhart picture Jake Gearhart · Sep 19, 2012 · Viewed 9.4k times · Source

Hello I am working on a C++ program and I am just starting out by creating some sample objects out of the class that I created. I am creating the object but for some reason the dot operator is not working with the object

This is the object call

Card testcard(Suit hearts, Value six);

This is the constructor

Card::Card(Suit suit, Value facevalue)
{
Card::suit=suit;
Card::faceValue=facevalue;
};

However the dot operator is not working, as if the object is not really there

I am controlling most of the program in seperate parts so there are many instances of header files which is where the card class is located, I am not sure if that is part of the problem

Answer

nneonneo picture nneonneo · Sep 19, 2012

From within an instance method, you can't use a dot to access the instance.

Try this-> instead:

Card::Card(Suit suit, Value facevalue)
{
    this->suit=suit;
    this->faceValue=facevalue;
};

Alternately, you can use an initializer list:

Card::Card(Suit suit, Value facevalue) : suit(suit), faceValue(facevalue)
    { }