>>
宣言および定義されたオーバーロードされた抽出演算子を持つオブジェクト (クラス) に文字列ストリームを渡そうとしています。たとえば、object1 のオーバーロードされた抽出演算子の宣言は次のとおりです。
friend istream& operator >>(istream& in, Object1& input);
object2 では、私の宣言はほぼ同じです
friend istream& operator >>(istream& in, Object2& input);
object1 抽出関数の間、func. 行を取得し、それを文字列ストリームに変換し、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 に変換する必要があるためですか? もしそうなら、どうすればいいですか?
最小限のプログラムは次のようになります。
#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.
}