使用运算符>>将字符串流传递给 iStream
passing a stringstream to istream using operator >>
我试图将字符串流传递到一个对象(类),该对象(类)具有声明和定义的重载提取操作符>>
。例如,object1中重载提取操作符的声明为
friend istream& operator >>(istream& in, Object1& input);
在object2中,我的声明几乎相同
friend istream& operator >>(istream& in, Object2& input);
在object1提取函数中,函数。获取一行,将其转换为stringstream,并尝试使用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转换为iststream吗?如果有,我该怎么做?
最小程序是这样的:在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.
}
以下代码可以正常编译:
#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版本之前声明?
相关文章:
- 为什么istream不支持右值提取
- 将无符号char*转换为std::istream*C++
- 需要从 istream 和 ostream 派生 iostream
- 使用std::istream::peek()总是安全的吗
- EASTL矢量<向量<int>>连续的
- 使用 istream 参数读取的 istream 函数
- istream std::cin如何修改自定义istream缓冲区
- 为什么我在使用 istream 迭代器时会出现 seg 错误?
- 在清除 istream 之前,我不应该需要取消获取它吗?
- C++,如何根据运行时条件构造引用不同 istream 对象的对象?
- 如何将QByteArray转换为std::istream或std::ifstream?
- 逗号在重载 ostream 和 istream 在结构中分开
- 为什么 Clang std::ostream 写一个 std::istream 无法读取的双精度?
- 从函数返回 istream 的正确方法
- 阅读从 istream 到矢量的不同类型列表
- 关于 istream 中的错误处理的问题
- C++ 模板类无法修复 ostream 和 istream 函数
- 如何使 std::istream 从键盘获取输入(如 std::cin)?
- iStream 运算符跳过空格字符
- 如何在 c++ 中将 IStream 转换为 Base64 字符串