Reputation: 21
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
Upvotes: 0
Views: 2028
Reputation: 44268
When you compile a .cpp file compiler only sees that file and headers that you directly or indirectly include. If you have one function from one .cpp file calling another one from another file - that resolved by different program called linker. So before that happens you must let compiler know that such function exists somewhere. In your case when compiling main.cpp
there is no information about operator<<
for class date
so you need to declare it somewhere to make it visible for compiler when it compiles main.cpp:
ostream& operator<<(ostream& ot, const date& d); //declaration, tells compiler such function exists somewhere
and best place for that is the header.
Now if you try to put this function definition rather that declaration in the header you will get another problem - both .cpp files will see it and function will be defined twice which is a error. You may avoid problem with marking that function inline
and then it is fine to define it multiple times in each compilation unit. So either you need to declare it in the header and define only in one cpp file, or you can define it inline in the header.
Upvotes: 0
Reputation: 218138
Declaration of ostream& operator<<(ostream& ot, const date& d);
should be in header.
Upvotes: 1