简体   繁体   中英

passing a stringstream to istream using operator >>

I am trying to pass a stringstream into an object(class) that has an overloaded extraction operator >> that is declared and defined. For example, the declaration for the overloaded extraction operator in object1 is

friend istream& operator >>(istream& in, Object1& input);

In object2, my declaration is almost the same

friend istream& operator >>(istream& in, Object2& input);

During the object1 extraction function, the func. gets a line, turns it into a stringstream and tries to use the 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 is designed to take a part of data
     stringstream ss(token); // I copied token into ss
     ss >> secondObj; // This is where I run into problems.
}

I get the error No match for operator >> . is this because I need to convert stringstream to istream? If so, how would I do that?

The minimal program would look like 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 I made the 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 is designed to take a part of data
     stringstream ss(token); // I copied token into ss
     ss >> secondObj; // This is where I run into 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 that I have this function in Object2.h
     }
}

The Object2.h file looks similar to Object1.h except:

template<class T>
class Object2{
public:
//similar istream and 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 is the private member variable in Object2.
                //it is of a 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 be elsewhere

Can you post a complete minimal self-contained program demonstrating the problem? Or just your declaration and definition of the function istream& operator >>(istream& in, Object2& input) ? Is it declared ahead of the Object1 version in the translation unit?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM