2013-09-24 85 views
1

所以,我有一些我是定義類,它給了我這個錯誤:沒有匹配函數調用[類]

#include <iostream> 
using namespace std;; 

//void makepayment(int amount, string name, Date date); 
//void listpayments(); 

class Date; 

class Date { 
    public: 
    int month; 
    int day; 
    int year; 
    Date(int month, int day, int year) { 
     this->month = month; 
     this->day = day; 
     this->year = year; 
    } 
}; 

class Payment { 
    public: 
    int amount; 
    string name; 
    Date date; 
    Payment(int amount, string name, Date date) { 
     this->amount = amount; 
     this->name = name; 
     this->date = date; 
    } 
}; 

int main() { 
cout << 
"|~~~~~~~~~~~~~~~~~~~~~~~~| \n" << 
"| WELCOME TO THE  | \n" << 
"| WESSLES BANK   | \n" << 
"| MANAGEMENT SYSTEM! | \n" << 
"|~~~~~~~~~~~~~~~~~~~~~~~~| \n"; 

for(;;) { 

} 

return 0; 
} 

錯誤是:

foo.cpp: In constructor ‘Payment::Payment(int, std::string, Date)’: 
foo.cpp:26:49: error: no matching function for call to ‘Date::Date()’ 
foo.cpp:26:49: note: candidates are: 
foo.cpp:14:5: note: Date::Date(int, int, int) 
foo.cpp:14:5: note: candidate expects 3 arguments, 0 provided 
foo.cpp:9:7: note: Date::Date(const Date&) 
foo.cpp:9:7: note: candidate expects 1 argument, 0 provided 

我不知道什麼是錯的! '沒有匹配的通話功能'是什麼意思?

對不起,如果這是一個noobie問題...我剛開始C++。

回答

3

這是因爲您已嘗試複製對象,並且沒有提供默認構造函數。

this->date = date; 

,你應該做的事情,正在初始化在初始化列表一切。也沒有理由不應該通過引用來傳遞這些參數。

Payment(int amount, const string& name, const Date& date) 
    : amount(amount) 
    , name(name) 
    , date(date) 
    {} 

同樣適用於您的Date類。這將使用編譯器生成的拷貝構造函數。請注意,如果您的類包含更多的POD類型,您可能需要實現自己的拷貝構造函數。

+0

好。謝謝!我主要是一名java程序員。剛剛開始C++ tody:P。 – user1766728

+0

沒問題。用C++玩得開心! – Aesthete

0

因爲你在你的支付類有

Date date; 

,你必須有一個日期構造函數不帶任何參數,即日期::日期(),你沒有規範。

0

Payment類有一個Date子元素。 Payment構造函數首先嚐試使用默認構造函數實例化Date子項,然後爲此子項分配一個新值。問題是Date子項沒有默認的構造函數Date :: Date()。要麼給Date類默認構造函數或改變付款構造語法如下:

Payment::Payment(int amount_, string name_, Date date_) : amount(amount_), 
    name(name_), date(date_) 
{ 
} 

編輯:唯美主義者打我給它