当我遇到运算符重载问题时,我正要完成作业,错误提示
错误 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 返回 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;
}
最佳答案
在 .cpp
文件中,您不应该重新定义类,而是包含 header 并实现方法。因此,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;
}
缺少实现,运算符应在另一个 header (可能是 Date.h
)中声明,并且 operator =
应返回 Date&
,而不是日期
(尽管不是强制性的。
此外,如果您希望使用 3 个参数调用 Date
,您可能需要:
Date::Date (int day_, int month_, int year_ ) :
day(day_), month(month_), year(_year)
{
}
在您的实现文件中,并在 header 中声明此构造函数。
关于c++ - 编写 C++ 运算符重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10166863/