我重载了Extraction和Insertion运算符。但是现在无论何时在main.cpp中调用它们,编译器都会告诉我存在一个模糊错误。我有点理解歧义错误是什么,但我不能为我的生活弄清楚它为什么会发生。
如果有帮助,我会使用g ++编译器和LLDB调试器。
错误:
/Users/Final Project/ms1.cpp:147:12: error: use of overloaded operator '<<' is ambiguous (with operand types 'ostream' (aka 'basic_ostream<char>') and 'oop244::Date')
cout << A << " is not equal to " << B << " but operator!= returns otherwise!" << endl;
~~~~ ^ ~
/Users/Final Project/Date.h:57:17: note: candidate function
std::ostream& operator<<(std::ostream os, Date& Dob);
接下来是十亿其他类似的错误。
我的主要(发生错误的地方):
#include <iostream>
#include <cstring>
#include "Date.h"
#include "Date.cpp"
#include "general.h"
using namespace std;
using namespace oop244;
bool equalDates(const char* A, const char* B);
int main(){
int ret = 0;
char confirm[2000];
bool ok = true;
Date A(2018, 10, 18);
Date B;
......
......
A = Date(2018, 10, 17);
if (ok){
cout << "testing operator!=" << endl;
if (A != B){
cout << "passed!" << endl;
}
else{
cout << A << " is not equal to " << B << " but operator!= returns otherwise!" << endl;
ok = false;
}
}
班级宣言
#ifndef __244_DATE_H__
#define __244_DATE_H__
// header file includes
#include "general.h"
#include <string>
#include <iostream>
namespace oop244{
class Date{
private:
// private member variables
int _year;
int _mon;
int _day;
int _readErrorCode;
// private member functions and setters
int value()const;
void setErrCode(int errCode);
.....
.....
public:
// constructors
Date();
Date(int,int,int);
~Date();
std::ostream& write(std::ostream& ostr, Date& dob);
std::istream& read(std::istream& istr, Date& dob);
};
std::istream& operator>>(std::istream is, Date& dob);
std::ostream& operator<<(std::ostream os, Date& dob);
}
#endif
类函数定义
#include "general.h"
#include "Date.h"
#include <iomanip>
#include <iostream>
using namespace std;
namespace oop244{
// constructors
Date::Date()
{
_year=0;
_mon=0;
_day=0;
_readErrorCode=NO_ERROR;
}
Date::Date(int year,int month, int day)
{
_year=year;
_mon=month;
_day=day;
_readErrorCode=NO_ERROR;
}
Date::~Date() {}
// member functions
void Date::setErrCode(int errCode)
{
_readErrorCode=errCode;
}
void Date::passErr(int errCode)
{
setErrCode(errCode);
}
.....
.....
int Date::getErrCode() const
{
return _readErrorCode;
}
std::istream& Date::read(std::istream& istr, Date& dob)
{
char y[5],m[3],d[3];
int year,month,day;
istr.getline(y,5,'/');
istr.getline(m,3,'/');
istr.getline(d,3,'/');
year=atoi(y);
month=atoi(m);
day=atoi(d);
Date temp (year,month,day);
if (year>=2000 && year<=2030)
dob.passErr(YEAR_ERROR);
else if (month+=1 && month<=12)
dob.passErr(MON_ERROR);
else if (day>=1 && day<=31)
dob.passErr(DAY_ERROR);
else
{
dob.passErr(NO_ERROR);
dob=temp;
}
return istr;
}
std::ostream& Date::write(std::ostream& ostr, Date& dob)
{
int year=dob.getYear();
int month=dob.getMonth();
int day=dob.getDay();
ostr<<year<<"/"<<month<<"/"<<day<<endl;
return ostr;
}
// non-memeber operator overloads
std::istream& operator>>(std::istream& is, Date& dob)
{
dob.read(is,dob);
return is;
}
std::ostream& operator<<(std::ostream& os, Date& dob)
{
dob.write(os,dob);
return os;
}
}
对不起超长的帖子。我的智慧结束了。我提前感谢你们。
答案 0 :(得分:2)
看起来像运算符&gt;&gt;的函数原型和运算符&lt;&lt;不要将他们的参数与定义相匹配。换句话说:
std::ostream& operator<<(std::ostream os, Date& dob);
你头文件中的缺少&amp;对于std :: ostream。对于运算符&gt;&gt;。
也是如此