2012-04-15 5 views
2

私はoperator-オーバーロードの問題を得たとき、私の割り当てを終えることを約あった、エラーがC++ operator-overloadを書いていますか? 「日::日」:

エラー1つのエラーC2661言う86 \ \ \ユーザー:なしオーバーロードされた関数を3つの引数cを取りますプロジェクト\ Visual Studioの2010 \ドキュメント\ assignmnent 3 840 \ assignmnent 3 840 \ date.cpp 137

2のIntelliSense:コンストラクタのインスタンス "日::日付は、" 引数リストのCと一致する:86の\ドキュメント\ \ユーザーを\ビジュアルスタジオ2010 \ projects \ assignmnent 3 840 \ assignmnent 3 840 \ date.cpp 137

とそのマーキング返される日付Date(mt、dy、yr);助けてください、私はすでに3時間この事を試しています。ここ

.cppファイル内のコード

////////////////////date.h 


#include <iostream> 
using namespace std; 


class Date 
{ 
private: 
int day,month,year ; 

public: 

Date(); 
void setValues(); 
// int getValues() ; 
Date operator=(const Date &); 
// 
Date(const Date &); 
// 

//friend  Date operator+(Date a,Date b); 
Date operator-(const Date &); 


friend bool operator>(Date a, Date b); 
friend bool operator==(Date a, Date b); 
friend ostream &operator<<(ostream &out, Date a); 
friend istream &operator>>(istream &in, Date &a); 




// 



}; 

/////////////////date.cpp 

#include <iostream> 
using namespace std; 


class Date 
{ 
private: 
int day,month,year ; 

public: 

Date(); 
void setValues(); 
// int getValues() ; 
Date operator=(const Date &); 
// 
Date(const Date &); 
// 

//friend  Date operator+(Date a,Date b); 
Date operator-(const Date &); 


friend bool operator>(Date a, Date b); 
friend bool operator==(Date a, Date b); 
friend ostream &operator<<(ostream &out, Date a); 
friend istream &operator>>(istream &in, Date &a); 




// 



}; 

////////driver.cpp 
//test.cpp 
#include "date.h" 
#include <iostream> 
using namespace std; 
int main() 
{ 
Date date1; 
Date date2 = date1; //copy constructor called 

cout << "Initial date values\n"; 
cout << "Date 1 is "; 
cout << date1 << endl; 
cout << "Date 2 is "; 
cout << date2 << endl; 
cout << "Enter a date no earlier than 1800\n"; 
cin >> date1; 




cout << "Enter another date no earlier than 1800\n"; 
cin >> date2; 
cout << "Revised date values\n"; 
cout << "Date 1 is "; 
cout << date1 << endl; 
cout << "Date 2 is "; 
cout << date2 << endl; 

if (date1 == date2) 
cout << "The two input dates are the same\n"; 
else if (date1 > date2) 

{ 
cout << "Date 1 is later in time than Date 2 by "; 
Date temp = date1 - date2; 
cout << temp << endl; 
} 
else 
{ 
cout << "Date 2 is later in time than Date 1 by "; 
Date temp = date2 - date1; 
cout << temp << endl; 
} 



//Date date3, date4; 
//date4 = date3 = date2; //overloaded assignment operator called 
//cout << "After the assignment date4 = date3 = date2\n"; 
//cout << " Date 3 is " << date3 << " and Date 4 is " << date4 << endl; 
return 0; 
} 
+1

コールサイトを提供できますか?あなたが実際にオペレータを呼んでいるライン? – smichak

答えて

2

である、あなたは、クラスを再定義しますが、ヘッダーを含めるとメソッドを実装することになっていません。だから、Date.hはOKですが、Date.cppはの線に沿って何かする必要があります:実装が不足している

//Date.cpp 
#include "Date.h" 

Date::Date() 
{ 
} 

void Date::setValues() 
{ 
} 
Date Date::operator=(const Date &) 
{ 
    return *this; 
} 
Date::Date(const Date &) 
{ 
} 
Date Date::operator-(const Date &) 
{ 
    return *this; 
} 
bool operator>(Date a, Date b) 
{ 
    return true; 
} 
bool operator==(Date a, Date b) 
{ 
    return true; 
} 
ostream &operator<<(ostream &out, Date a) 
{ 
    return out; 
} 
istream &operator>>(istream &in, Date &a) 
{ 
    return in; 
} 

Date事業者はDate.hおそらく、別のヘッダ内で宣言されなければならない、とoperator =Date&を返すべきではなく、 。あなたは3つのパラメータでDateを呼び出すために探している場合は(必須ではありませんけれども

また、あなたはおそらくしたい:

Date::Date (int day_, int month_, int year_) : 
    day(day_), month(month_), year(_year) 
{ 
} 

を実装ファイルに追加し、ヘッダーにこのコンストラクターも宣言します。

+0

私はhonesであれば何を意味するのか分かりません、より具体的にお願いしてください。私はこのプログラミングのすべてを開始しています。 – user1335175

+0

@ user1335175私はかなり特定でした。 '.cpp'ファイルでクラスを再定義する必要はありません。そこにメソッドを実装する必要があります。 –

+0

私は3つのパラメータのコンストラクタを追加して私のプログラムを修正してくれてありがとう:) – user1335175

関連する問題