大家帮忙看这个问题

mvv 2003-11-25 07:29:03
#include <iostream>
using namespace std;

class test
{
public:

test(int i):num(i){}
friend ostream& operator<< (ostream& ostr,const test& d);

private:
int num;

};

ostream& operator << (ostream& ostr, const test& t)
{
ostr << t.num<<endl;
return ostr;
}

int main(int argc, char *argv[])
{
test t(1);
cout<<t;

return 0;
}

编译过不了,把
#include <iostream>
using namespace std;
这两局改成
#include <iostream.h>,编译就过了。

如果我一定要用C++的<iostream>,不用<iosream.h>,大家看该怎么修改?
...全文
51 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mvv 2003-11-26
  • 打赏
  • 举报
回复
可以结帖了吧
mvv 2003-11-26
  • 打赏
  • 举报
回复
经过我修改,只要把友员函数定义在类定义里边,就可以了,各位可以参考。
#include "stdafx.h"
#include <iostream>
using namespace std;

class test
{
public:

test(int i):num(i){}
friend ostream& operator<< (ostream& ostr,const test& t)
{
ostr << t.num<<endl;
return ostr;
}

private:
int num;

};

int main(int argc, char *argv[])
{
test t(3);
cout<<t;

return 0;
}
ningzhiyu 2003-11-25
  • 打赏
  • 举报
回复
估计是楼主的编译器太旧了吧。
现在可以下载的mingW的版本应该是3.23版本的了。
gcc/g++ 3.0以后的应该支持的很好了。
反正我编译了一下,还好啊。

据我所知用2.9*版本的gcc/g++的有
1。dev-c++ 4.0带编译器安装版
2。很多版本cygwin,好像没看见带3.0以上的编译器的(当然自己升级的例外)
3。redhat7.*以及以前版本的自带gcc/g++编译器

我用的是dev-cpp 4.9.8.0的IDE (就是dev-c++的高级版本)+ wingM编译器
如果楼主有兴趣可以到到www.sourceforge.net去下载一个。
分别搜索dev-cpp和wingM,就可以了。

playmud 2003-11-25
  • 打赏
  • 举报
回复
namespace是个好东东阿!
编译不过去是你的工具太老了。
freewing 2003-11-25
  • 打赏
  • 举报
回复
为什么都喜欢用namespace???
还"一定要用"....
liem 2003-11-25
  • 打赏
  • 举报
回复
friend ostream& operator<< (ostream& ostr,const test& d);

改成:
friend ostream& std::operator<< (ostream& ostr,const test& d);

ningzhiyu 2003-11-25
  • 打赏
  • 举报
回复
visual C++ 6.0是98、99年的产物吧?不是很清楚,不过比较早了。
gcc/g++ 3.0好像根据c99标准的……

要vc 6.0支持c++的新标准好难呢……在研发时,新的c++标准还没有成型吧
你用vc7试一下吧
mvv 2003-11-25
  • 打赏
  • 举报
回复
我用的是visual C++ 6.0,编译不过,难道它都不行??

64,654

社区成员

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

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