I have a header file for declaring the class, a cpp file for its methods definition and a main source file, in the main I've included the header file, but the compiler complains that I haven't defined his methods... Date.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
Date.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;
}
Errors: Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'date' (or there is no acceptable conversion) c:\users\aub\documents\visual studio 2013\projects\project22\project22\main.cpp 5
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
I've also tried to implement my overloaded operator<< in date.h instead but didn't work...
The errors I get after declaring operator<< in the header file instead are: Error 1 error LNK2005: "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class date const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVdate@@@Z) already defined in Date.obj c:\Users\aub\documents\visual studio 2013\Projects\Project22\Project22\main.obj
Error 2 error LNK1169: one or more multiply defined symbols found c:\users\aub\documents\visual studio 2013\Projects\Project22\Debug\Project22.exe 1