对于这个学校项目,我们需要一个卡类,其中包含int等级,char颜色和两个char *到C风格的字符串(用于操作和位置)。我们需要包含以下类:C++类中的构造函数
1)卡的默认构造函数(默认情况下,排名和位置)
2)卡参数的构造函数(所有数据成员作为参数)
3)身份证复印件构造函数
我不知道如何在类中包含所有这些。我不断收到编译器错误,如“候选人期望_参数,_给定”,候选人是:“然后列出我所有的构造函数。
我不知道如何在我的类中声明这些错误, ,怎么现在打电话给他们
我:
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;
};
我的构造函数:
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);
}
我得到的编译器错误(到目前为止)在地方Wh是ERE的构造函数的调用:
card first;
和
miniDeck = new card[ 4 ];
我知道我需要告诉编译器,其构造是哪个。但是如何?
您尚未实施默认构造函数。 – chris
另外,不要为实现中的参数提供默认参数;仅在类*声明*中提供。 – WhozCraig
@WhozCraig - 这使所有的错误消失。谢谢。不确定所有的语法。 – Bobazonski