2013-10-24 52 views
0

对于这个学校项目,我们需要一个卡类,其中包含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 ]; 

我知道我需要告诉编译器,其构造是哪个。但是如何?

+0

您尚未实施默认构造函数。 – chris

+1

另外,不要为实现中的参数提供默认参数;仅在类*声明*中提供。 – WhozCraig

+0

@WhozCraig - 这使所有的错误消失。谢谢。不确定所有的语法。 – Bobazonski

回答

3

问题是你实际上没有默认的构造函数。类的默认构造函数是没有任何参数传入的,例如它有空的参数列表,所以它的签名看起来像card()

//编辑:

现在我看到你试图在默认的card::card(const int newRank = -1, const char* newLocation = "location")参数值。然而,这是不正确的,你需要在它的头部的方法声明中进行,而不是在方法定义中。这应该可以解决你的问题。编辑

//最终只给你一些好的建议,有你可以遵循,以提高总体代码(虽然这并不涉及到代码的正确性)一些好的做法:首先,了解有关initialization list以及如何使用它们。其次 - 即使这不同于程序员,程序员和项目,也不一定要使用起始大写字母(Upper骆驼大小写)来表示类名。