Thursday, 15 September 2011

c++ - Operator overloading error? -



c++ - Operator overloading error? -

i maintain getting compiler error programme in visual studio express 13 commented 2 lines in code compiler error showing up

date.cpp

#include "date.h" using namespace std; date::date(int d, int m, int y) : day(d), month(m), year(y) {} date::date() : day(0), month(0), year(0) {} const int date::getday() { homecoming day; } const int date::getmonth() { homecoming month; } const int date::getyear(){ homecoming year; } bool date::operator<(const date dother) { homecoming (year < dother.year) || (year == dother.year && month < dother.month) || (year == dother.year && month == dother.month && day < dother.day); } string date::tostring() //error: declaration incompatible with...declared @ line 21...date.h { string s = month + "-" + day; s+="-" + year; homecoming s; } ofstream& operator<<(ofstream& out, date& d) { string s = d.tostring(); out.write(s.c_str(), s.size()); homecoming out; } void date::operator=(string s) //no instance of overloaded function "date::operator=" matches specified type { stringstream stream; stream << s; stream >> month; stream >> day; stream >> year; }

date.h

#ifndef date_class #define date_class #include <iostream> #include <string> #include <fstream> #include <sstream> class date { private: int day, month, year; public: date(); date(int, int, int); const int getday(); const int getmonth(); const int getyear(); string tostring(); bool operator<(const date); friend ofstream& operator<<(ofstream& out, date&d); void operator=(string); }; #endif

i have no thought why these errors showing up. problem operator overloading? or visual studio(for reason if delete code in date.cpp, compiler errors disappear)?

you have namespaces mismatch. in header not utilize std namespace prefix before string compiler can't find type string need.

c++ operator-overloading

No comments:

Post a Comment