我遇到构造函数问题,我需要创建2个构造函数。 1.无参数构造函数将对象设置为安全的空状态。 2.双参数构造函数
当我为对象变量赋值时,我收到错误。
CreditCard.cpp: In constructor âCreditCard::CreditCard()â:
CreditCard.cpp:8:11: error: invalid use of member (did you forget the â&â ?)
CreditCard.cpp: In constructor âCreditCard::CreditCard(long long int, char*)â:
CreditCard.cpp:15:11: error: invalid use of member (did you forget the â&â ?)
CreditCard.cpp: In member function âlong long int CreditCard::number() constâ:
CreditCard.cpp:32:12: error: argument of type âlong long int (CreditCard::)()constâ does not match âlong long intâ
1.CreditCard();
CreditCard::CreditCard() {
number=0;
strcpy(holder,"ABCD");
balances=0;
}
2。 CreditCard(2个参数)
CreditCard::CreditCard(long long num,char h[21]) { number=num; strcpy(holder,h); }
第3。第3次错误
long long CreditCard::number() const { return number; }
Class(在头文件中)
class CreditCard {
long long int nubmer;
char holder[21];
double balances;
public:
/*declarations of constuctors */
CreditCard();
CreditCard(long long num,char h[21]);
void charge(double c);
void pay(double p);
long long number() const;
double balance() const;
const char* name() const;
};
更新:与成员变量和函数(名称)混淆
答案 0 :(得分:3)
long long int nubmer;
仔细看看那条线。
long long CreditCard::number() const {
return number;
}
此函数声称它返回long long
,但实际上它会尝试返回函数。这应该如何运作?