ostringstream传参数ostream编译错误,速结,在线

whslovexyp 2011-04-21 10:54:35
函数参数是ostream& arg, 我传入ostringstream类型的参数,编译错误
cannot convert parameter 1 from 'class std::basic_ostringstream<char,struct std::char_traits<char>,class std::allocator<char> >' to 'class ostream &'

for example:
test(ostream& arg);

ostringstream oss;
test(oss);

然后编译错误如上,请问为什么?ostringstream不是ostream的子类吗?我传入ofstream是可以的,郁闷啊。
...全文
155 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
whslovexyp 2011-04-21
  • 打赏
  • 举报
回复
问题是我还是出了这个编译错误,不知道哪里的问题
pengzhixi 2011-04-21
  • 打赏
  • 举报
回复
ostringstream是ostream的派生类。
pathuang68 2011-04-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hnuqinhuan 的回复:]

C/C++ code

#include <iostream>
#include <fstream>
#include <sstream>
#include <ostream>
using namespace std;
void test(ostream& arg)
{}
int main()
{

ostringstream oss;
test(oss);
……
[/Quote]

++
whslovexyp 2011-04-21
  • 打赏
  • 举报
回复
vc++ 6.0

template<class _E,
class _Tr = char_traits<_E>,
class _A = allocator<_E> >
class basic_ostringstream : public basic_ostream<_E, _Tr> {
这里不是有继承关系吗
無_1024 2011-04-21
  • 打赏
  • 举报
回复
ostringstream不是ostream的子类
所以根据继承关系
ostream &函数可以用用ofstream和ostringstream代替
istream &函数可以用ifstream个istringstream代替

补充一下 上面的例子VS2010编译运行正常 应该是别的地方出错
無_1024 2011-04-21
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
#include <sstream>
#include <ostream>
using namespace std;
void test(ostream& arg)
{}
int main()
{

ostringstream oss;
test(oss);
return 0;
}

//完全可以啊 VC6.0
pengzhixi 2011-04-21
  • 打赏
  • 举报
回复
不行就换成指针吧
whslovexyp 2011-04-21
  • 打赏
  • 举报
回复
结贴吧,不知道原因,只能强制转换下
pengzhixi 2011-04-21
  • 打赏
  • 举报
回复
那就换编译器呗!
Nueva 2011-04-21
  • 打赏
  • 举报
回复
我刚查过资料,在C++ Primer 4Th的定义中,如果函数的形参是ofstream或者ostringstream,则可以给函数传递ostream实参,理论上来说,可以的
無_1024 2011-04-21
  • 打赏
  • 举报
回复
应该是当前工程别破坏了
whslovexyp 2011-04-21
  • 打赏
  • 举报
回复
郁闷了,不知道哪里问题。新建一个工程就没有问题,不知道原因了。
luciferisnotsatan 2011-04-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hnuqinhuan 的回复:]

C/C++ code

#include <iostream>
#include <fstream>
#include <sstream>
#include <ostream>
using namespace std;
void test(ostream& arg)
{}
int main()
{

ostringstream oss;
test(oss);
……
[/Quote]
vs2005也没错

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧