C++ 使用运算符将stringstream传递到istream>&燃气轮机;

C++ 使用运算符将stringstream传递到istream>&燃气轮机;,c++,operators,overloading,stringstream,extraction,C++,Operators,Overloading,Stringstream,Extraction,我试图将stringstream传递到一个对象(类)中,该对象(类)具有声明和定义的重载提取运算符>。例如,object1中重载提取运算符的声明是 friend istream& operator >>(istream& in, Object1& input); 在object2中,我的声明几乎相同 friend istream& operator >>(istream& in, Object2& input); 在ob

我试图将stringstream传递到一个对象(类)中,该对象(类)具有声明和定义的重载提取运算符
>
。例如,object1中重载提取运算符的声明是

friend istream& operator >>(istream& in, Object1& input);
在object2中,我的声明几乎相同

friend istream& operator >>(istream& in, Object2& input);
在object1提取函数期间,func。获取一行,将其转换为stringstream,并尝试使用Object2的extraction(>>)运算符

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.
}
我得到了与操作员不匹配的错误
。这是因为我需要将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”
#包括“Object2.h”
#包括“dataClass.h”
使用名称空间std;
int main(){
目标1第一目标;
cin>>第一个OBJ;
库特达;
in.ignore();
token=GetToken(data,,,someint);//这是为获取部分数据而设计的
stringstream ss(令牌);//我将令牌复制到了ss中
ss>>secondObj;//这就是我遇到问题的地方。
}
模板
ostream和操作员数据//data是Object2中的私有成员变量。
//它属于模板化类类型。
}

以下内容可以很好地编译:

#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;
}
#包括
#包括
#包括
使用名称空间std;
结构X{};
结构Y{};
istream&运算符>>(istream&,X&){}
istream和运算符>>(istream和,Y和)
{
stringstream ss(“foo”);
X;
ss>>x;
}
int main()
{
Y;
cin>>y;
}
你的问题一定在别处


你能发布一个完整的最小独立程序来演示这个问题吗?或者只是您对函数的声明和定义
istream&operator>>(istream&in,Object2&input)
?在翻译单元中,它是在Object1版本之前声明的吗?

@Bruce:这还不够。通过将使用过的部分提取到一个小测试程序中,将问题一分为二。如果有效,继续添加内容,直到它停止工作。如果测试程序不起作用,就把它发布在这里。我发布了更多的信息。我正在制作一个模板化的linkedlist。我不知道这些错误是否是因为类是模板化的。
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.
}
#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;
}