2014-02-07 207 views
0

我遇到了構造函數的問題,我需要創建2個構造函數。 1.將參數設置爲安全空狀態的無參數構造函數。 2.雙參數的構造函數C++錯誤,類和構造函數

我得到的錯誤,當我分配一個值的對象變量。

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.信用卡式(2個參數)

CreditCard::CreditCard(long long num,char h[21]) { 
    number=num; 
    strcpy(holder,h); 
} 

3.第三錯誤

long long CreditCard::number() const { 

    return number; 
} 

類(在頭文件)

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; 
}; 

UPDATE:得到混合了成員變量和函數(名稱)在該行

回答

3
long long int nubmer; 

仔細查看。

long long CreditCard::number() const { 

    return number; 
} 

此功能聲稱它返回一個long long,但它實際上試圖返回一個功能。這應該如何工作?

+0

就是這樣。成員_variable_名稱中的單純的拼寫錯誤不會導致此錯誤。正確的名稱「數字」實際上是一個功能,確實如此。 –

+0

兩種錯誤的組合比單獨一種診斷困難得多。 –

+0

Welp,更快一點,並在正確的解釋中編輯。 +1 – Xarn