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); 请帮忙,我已经尝试这个东西 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,并且operator =应该返回 a Date&,而不是 a Date(尽管不是强制性的。

此外,如果您希望Date使用 3 个参数进行调用,您可能需要:

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

在您的实现文件中,并在标题中声明此构造函数。

于 2012-04-15T22:54:24.540 回答