Reputation: 1565
For this school project, we need a card class that holds int rank, char color, and two char* to C-style strings (for action and location). We need the class the contain the following:
1) Card default constructor (default the rank and location)
2) Card parameterized constructor (all the data members as parameters)
3) Card copy constructor
I have no idea how to include all of these in the class. I keep getting compiler errors like "candidate expects _ arguments, _ given", candidates are:" then listing all my constructors.
I don't know how to declare these in my class, how to title them in their implementation, and how to call them.
Right now I have:
class card
{
public:
card(const int, const char*);
~card();
card(const card&);
card(const int, const char, const char*, const char*);
void copyCard(const card&);
void print();
void setColor(const char);
void setRank(const int);
void setAction(const char*);
void setLocation(const char*);
char getColor();
int getRank();
char* getAction();
char* getLocation();
private:
char color;
int rank;
char* action;
char* location;
};
my constructors:
card::card(const int newRank = -1, const char* newLocation = "location"){
color='c';
rank=newRank;
action = new char[7];
stringCopy(action, "action");
location = new char[9];
stringCopy(location, newLocation);
}
card::card(const card &newCard){
int length;
color = newCard.color;
rank = newCard.rank;
length = stringLength(newCard.action);
action = new char[length+1];
length = stringLength(newCard.location);
location= new char[length+1];
stringCopy(action, newCard.action);
stringCopy(location, newCard.location);
}
card::card(const int newRank, const char newCol, const char* newAct,
const char* newLoc){
int length;
color = newCol;
rank = newRank;
length = stringLength(newAct);
action = new char[length+1];
length = stringLength(newLoc);
location = new char[length+1];
stringCopy(action, newAct);
stringCopy(location, newLoc);
}
I get the compiler errors (so far) at the places where the constructors are called:
card first;
and
miniDeck = new card[ 4 ];
I know I need to tell the compiler which constructor is which. But how?
Upvotes: 0
Views: 207
Reputation: 656
The problem is that you actually do not have default constructor. Default constructor of a class is such that is does not have any paremeters passed in, e.g. it has empty parameter list so its signature would look something like card()
.
//Edit:
Now I see that you are trying to default your parameter values in card::card(const int newRank = -1, const char* newLocation = "location")
. This is incorrect however, you need to do this in the method declaration it he header, not in the method definition. That should solve your problem.
//End of edit
Just to give you some good tips, there are few good practices you could follow to improve your code in general (although this does not relate to correctness of your code): Firstly, find out about initialization list and how they are used. Secondly - even though this differs from programmer to programmer and project to project - use names with starting capital letters (Upper camel case) for your classes.
Upvotes: 3