【问题标题】:passing a stringstream to istream using operator >>使用运算符将​​字符串流传递给 istream >>
【发布时间】:2013-04-13 11:17:47
【问题描述】:

我试图将一个字符串流传递给一个对象(类),该对象(类)具有一个已声明和定义的重载提取运算符>>。例如,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 吗?如果是这样,我该怎么做?

最小程序如下所示: 在 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.
}

【问题讨论】:

    标签: c++ operators overloading stringstream extraction


    【解决方案1】:

    以下编译正常:

    #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&amp; operator &gt;&gt;(istream&amp; in, Object2&amp; input) 的声明和定义?它是否在翻译单元中的 Object1 版本之前声明?

    【讨论】:

    • @Bruce:这还不够。通过仅将使用过的部分提取到一个小型测试程序中来将问题一分为二。如果可行,请继续添加内容,直到它停止工作。如果测试程序不起作用,请在此处发布。
    • 我发布了更多信息。我正在处理一个模板链表。我不知道错误是否发生,因为类是模板化的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2015-06-13
    • 2012-06-13
    • 2020-04-08
    • 2016-06-01
    • 2011-08-16
    相关资源
    最近更新 更多