Possible Duplicate:
Why is it an error to use an empty set of brackets to call a constructor with no arguments?
Constructing Objects and Calling Member functions
Recently I've learnt a bit about constructors but today i've been having trouble setting default values for the constructor parameter.
The constructor declaration in the hpp file looks like this:
class PlayingCard {
public:
PlayingCard(int value = 0, int suit = 0); //default values supplied
and the definition in the cpp file looks like this:
PlayingCard::PlayingCard(int value, int suit) : _faceValue(value), _suit(suit)
{}
(_faceValue
and _suit
are private members of PlayingCard)
When i go to test this like so:
PlayingCard card1();
PlayingCard card2(7, 1);
cout << "suit int value: " << card1.getSuit() <<
" face value: " << card1.getFaceValue() << endl;
cout << "suit int value: " << card2.getSuit() <<
" face value: " << card2.getFaceValue() << endl;
card2 works fine, if i comment out the code in relation to card1. Otherwise i get the error: request for member 'getSuit' in 'card1', which is of non-class type 'PlayingCard()'.
Which must mean it doesn't recognized use of the constructor without arguments? What am i doing wrong?
PlayingCard card1();
does not declare an object, it declares function.
It declares an function named card1
which takes no parameters and return an object of the type PlayingCard
.
To declare an object you should do:
PlayingCard card1;
This is known as Most Vexing Parse in C++.
I am answering this second time today. Vexing Parse day it seems!