class - Issue overloading >> and << operator in C++ -
so have few weird things going on. if seek run programme default constructor, tells me can't find match >> overload. , when pass << overload it's spitting out trash. source below.
#include "employe.h" using namespace std; int main(int argc, char** argv) { employe unemploye(""); cin >> unemploye; cout << unemploye << endl; system("pause"); homecoming 0; }
employe.h
//employe.h #ifndef employe_h #define employe_h #include <iostream> class employe { protected: char * nom; public: employe(); employe(char *); void setnom(char * _nom); char * getnom(); //over load friend std::istream &operator>>(std::istream &input, employe &e); friend std::ostream& operator<< (std::ostream&, const employe &); }; #endif
and lastly employe.cpp
//employe.cpp #include<cstring> #include "employe.h" using namespace std; void employe::setnom(char * _nom) { this->nom = _nom; } char * employe::getnom() { homecoming this->nom; } employe::employe() { nom = new char[]; nom = ""; } employe::employe(char * _nom) { nom = _nom; } istream &operator>>(istream &input, employe &e){ char temp[1024]=" "; input >> temp; e.setnom(temp); homecoming input; } ostream& operator<< (ostream &out, employe const &e) { out << "je suis united nations employer, et mon nom est :" << e.nom << "."; homecoming out; }
example input: test illustration output:
if programme run default constructor , not empty string, error: error 1 error c2679: binary '>>' : no operator found takes right-hand operand of type 'employe (__cdecl *)(void)' (or there no acceptable conversion)
c++ class operator-overloading
No comments:
Post a Comment