передача потока строк в istream с помощью оператора & gt; & gt;

Я пытаюсь передать поток строк в объект (класс), который имеет перегруженный оператор извлечения >> это объявлено и определено. Например, объявление для перегруженного оператора извлечения в object1:

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

В object2 мое объявление почти такое же

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

Во время функции извлечения object1, функция. получает строку, превращает ее в поток строк и пытается использовать оператор извлечения (>>) объекта 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.
}

Я получаю ошибку No match for operator >>, это потому что мне нужно конвертировать stringstream в istream? Если так, как бы я это сделал?

Минимальная программа будет выглядеть так:
в main.cpp:

#include "Object1.h"#include "Object2.h"#include "dataClass.h"using namespace std;
int main(){
Object1<dataClass> firstObj;
cin>>firstObj;
cout<<firstObj<<endl;
}

в 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
}
}

Файл Object2.h похож на Object1.h за исключением:

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.
}

3

Решение

Следующее компилируется нормально:

#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;
}

Ваша проблема должна быть в другом месте

Можете ли вы опубликовать полную минимальную автономную программу, демонстрирующую проблему? Или просто ваше объявление и определение функции istream& operator >>(istream& in, Object2& input) ? Объявлен ли он перед версией Object1 в модуле перевода?

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector