2

オペレーターのオーバーロードで問題が発生したとき、割り当てを終了しようとしていました。エラーは次のように述べています

エラー 1 エラー C2661: 'Date::Date': 3 つの引数を取るオーバーロードされた関数はありません c:\users\86\documents\visual studio 2010\projects\assignmnent 3 840\assignmnent 3 840\date.cpp 137

2 IntelliSense: コンストラクター "Date::Date" のインスタンスが引数リスト c:\users\86\documents\visual studio 2010\projects\assignmnent 3 840\assignmnent 3 840\date.cpp 137 と一致しません

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

ここにコードがあります

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

1 に答える 1

2

ファイルでは.cpp、クラスを再定義することは想定されていませんが、ヘッダーを含めてメソッドを実装します。だから、Date.h大丈夫ですが、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.hであり、 aではなく aoperator =を返す必要があります (ただし、必須ではありません。Date&Date

また、Date3 つのパラメーターで呼び出したい場合は、おそらく次のようにします。

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

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

于 2012-04-15T22:54:24.540 に答える