Pergunta

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

Foi útil?

Solução

Assuming the class looks something like:

class Card {
public:
    Card (Suit argSuit, Value argFaceValue);

private:
    Suit   m_Suit;
    Value  m_FaceValue;
};

The constructor would look like this. Since the members of the class are available to the objects created from the class you do not need to do anything special to access them. The members of the class are within scope and are visible. You just need to make sure that the argument list of the function uses different symbols or names than the class/object members.

Card::Card(Suit argSuit, Value argFaceValue)
{
    m_Suit = argSuit;
    m_FaceValue = argFaceValue;
}

Outras dicas

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)
    { }

This is the object call

Card testcard(Suit hearts, Value six);

No, that's a function declaration. Naturally using the member access operator (.) on the name of a function doesn't work, the function has its own type which doesn't have members.

To declare an automatic variable (creating a new object instance), don't repeat parameter types. Just say:

Card testcard(hearts, six);

I think some of your naming conventions are causing some confusion for you. As pointed out above, you are running into a problem of context with the suit and facevalue arguments passed to the constructor. This means that in the context of the constructor method, suit really means the suit that is passed in as an argument, over the suit that is a member variable of the class Card. People generally use a naming convention to help avoid this confusion, such as putting an m_ in front of each data member for the class, such that suit would become m_suit. You can use whatever you like, but that way someone else code would know right away that m_suit was a data member.

Another point is that you can, and probably should, initialize the data member for a class prior to the code for the constructor is executed. This is called the "initialization list" and would be done as follows (assuming you changed over to the naming convention above).

Card::Card (Suit suit, Value facevalue)
 : m_suit (suit), m_facevalue (facevalue)
{
    // no code needs to go here
}

For efficiency reasons it is a good idea to get in this habit. Further, using the this pointer is generally not a good idea in a constructor. It's not illegal, but can get you into trouble.

As far as the dot operator not working, it wasn't clear from your question exactly where you were using the dot operator. If you are referring to the :: syntax above, that isn't really an operator, it is part of the C++ syntax indicating that you are dereferencing the class. That syntax would work if you had declared the suit and facevalue data members as static, but that is not what you wanted to do.

You mention that you are declaring the Card class in multiple header files -- that too is bad.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top