c++ - passing a stringstream to istream using operator >> -
i trying pass stringstream object(class) has overloaded extraction operator >>
declared , defined. example, declaration overloaded extraction operator in object1 is
friend istream& operator >>(istream& in, object1& input);
in object2, declaration same
friend istream& operator >>(istream& in, object2& input);
during object1 extraction function, func. gets line, turns stringstream , tries use extraction(>>) operator of object2.
istream& operator >>(istream& in, object1& input){ object2 secondobj; string data; string token; in>>data; in.ignore(); token = gettoken(data, ' ', someint); //this designed take part of data stringstream ss(token); // copied token ss ss >> secondobj; // run problems. }
i error no match operator >>
. because need convert stringstream istream? if so, how that?
the minimal program this: in main.cpp:
#include "object1.h" #include "object2.h" #include "dataclass.h" using namespace std; int main(){ object1<dataclass> firstobj; cin>>firstobj; cout<<firstobj<<endl; }
in object1.h:
#ifdef object1_h_ #define object1_h_ #include <iostream> #include <string> #include <cstddef> #include "object2.h" template<class t> class object1{ public: //assume made big 3 template<class u>friend istream& operator >>(istream& in, object1<u>& input); template<class u>friend ostream& operator <<(ostream& out, const object1<u>& output); private: object2<t>* head; }; template<class t> istream& operator >>(istream& in, object1<t>& input){ object2 secondobj; string data; string token; in>>data; in.ignore(); token = gettoken(data, ' ', someint); //this designed take part of data stringstream ss(token); // copied token ss ss >> secondobj; // run problems. } template<class t> ostream& operator <<(ostream out, const object1<t>& output){ object2<t>* ptr; while(getnextptr(ptr) != null){ cout<<ptr; ptr = getnextptr(ptr); //assume have function in object2.h } }
the object2.h file looks similar object1.h except:
template<class t> class object2{ public: //similar istream , ostream funcions of object1 //a getnextptr function private: t data; object2<t>* next; }; template<class t> istream& operator >>(istream& in, object2<t>& input){ in>>data; //data private member variable in object2. //it of templated class type. }
the following compiles fine:
#include <string> #include <sstream> #include <iostream> using namespace std; struct x{}; struct y{}; istream& operator>>(istream&, x&) {} istream& operator>>(istream&, y&) { stringstream ss("foo"); x x; ss >> x; } int main() { y y; cin >> y; }
your problem must elsewhere
can post complete minimal self-contained program demonstrating problem? or declaration , definition of function istream& operator >>(istream& in, object2& input)
? declared ahead of object1 version in translation unit?
Comments
Post a Comment