当我遇到操作员重载问题时,我即将完成我的任务,错误说
错误1错误C2661:'Date :: Date':没有重载函数需要3个参数c:\ users \ 86 \ documents \ visual studio 2010 \ projects \ assignmnent 3 840 \ assignmnent 3 840 \ date.cpp 137
2智能感知:没有构造函数的实例“Date :: Date”匹配参数列表c:\ users \ 86 \ documents \ visual studio 2010 \ projects \ assignmnent 3 840 \ assignmnent 3 840 \ date.cpp 137
及其标记日期返回日期(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;
}
答案 0 :(得分: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 =
应该返回Date&
,而不是Date
(尽管不是必需的)
另外,如果您希望使用3个参数调用Date
,则可能需要:
Date::Date (int day_, int month_, int year_ ) :
day(day_), month(month_), year(_year)
{
}
在您的实现文件中,并在标题中声明此构造函数。