【发布时间】:2016-06-10 14:44:16
【问题描述】:
我有一个用于声明类的头文件、一个用于定义其方法的 cpp 文件和一个主源文件,我主要包含了头文件,但编译器抱怨我没有定义他的方法。 .. 日期.h
#ifndef _DATE_
#define _DATE_
#include <iostream>
#include <exception>
#include <string>
using namespace std;
class date{
int day, month, year;
public:
date(int d, int m, int y) :day(d), month(m), year(y){}
int getDay() const{ return day; }
int getMonth() const { return month; }
int getYear() const { return year; }
bool operator==(const date& d) const{ return ((day == d.day) && (month == d.month) && (year == d.year)); }
bool operator>(const date& d) const;
bool operator<(const date& d) const { return !(*this>d || *this == d); }
ostream& print(ostream& os) const;
};
#endif
日期.cpp
#include "Date.h"
bool date::operator>(const date& d) const {
if (year > d.year) return true;
if (year < d.year) return false;
if (month>d.month) return true;
if (month < d.month) return false;
if (day>d.day) return true;
return false;
}
ostream& date::print(ostream& out) const {
if (day < 10) out << "0";
out << day << "/";
if (month < 10) out << "0";
out << month << "/";
out << year << endl;
return out;
}
ostream& operator<<(ostream& ot, const date& d) {
return d.print(ot);
}
main.cpp
#include "Date.h"
int main() {
date d(17, 10, 1996);
cout << d;
return 0;
}
错误: 错误 1 错误 C2679:二进制“
2 IntelliSense: no operator "<<" matches these operands
operand types are: std::ostream << date c:\Users\aub\Documents\Visual Studio 2013\Projects\Project22\Project22\main.cpp 5
我也尝试在 date.h 中实现我的重载运算符
在头文件中声明 operator & __cdecl operator &,class date const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std @@@std@@AAV01@ABVdate@@@Z) 已经定义在 Date.obj c:\Users\aub\documents\visual studio 2013\Projects\Project22\Project22\main.obj
错误 2 错误 LNK1169:找到一个或多个多重定义符号 c:\users\aub\documents\visual studio 2013\Projects\Project22\Debug\Project22.exe 1
【问题讨论】:
-
编译器不会在其他 cpp 文件中查找声明。
operator<<没有像其他文件一样在标头中声明并在实现文件中定义,是什么让operator<<如此特别? -
“我也尝试在 date.h 中实现我的重载运算符
-
[OT]:我会将
return !(*this>d || *this == d);替换为return d > *this;。 -
我认为这不能解决我的问题...
-
[OT]:避免
using namespace std;,尤其是在标题中。
标签: c++ header include operator-overloading