operator << 操作符重载

五号智能 2008-05-31 07:15:23

#include <iostream>

struct abc
{
int a;
std::string b;
double c;


friend ostream& operator << (ostream &out,abc &hot)
{
out << hot.a << hot.b << hot.c;

return out;
}
}

int main()
{
abc a;
a.a=3;
a.b="none";
a.c=3.204;
std::cout <<a<<endl;
}


编译不能功。。我刚学这东西,还不知道哪里错。。帮帮我吧 :(
...全文
261 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sai_deer 2008-12-17
  • 打赏
  • 举报
回复
#include <iostream>
#include <string> //string库头文件~
using namespace std; //对于小程序这样写方便~

struct abc
{
int a;
string b;
double c;

friend ostream& operator << (ostream &out, abc &hot)
{
out << hot.a << hot.b << hot.c;
return out;
}
};

int main()
{
abc a;
a.a = 3;
a.b = "none";
a.c = 3.204;
cout << a << endl;
return 0;
}

phoex 2008-06-26
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
struct abc
{
int a;
std::string b;
double c;


friend ostream& operator << (ostream &out,abc &hot)
{
out << hot.a << hot.b.c_str() << hot.c;

return out;
}
};

void main(void )
{
abc a;
a.a=3;
a.b="none";
a.c=3.204;
std::cout <<a<<endl;
}
wkh5432 2008-05-31
  • 打赏
  • 举报
回复
呵呵 顶下 学习中
taodm 2008-05-31
  • 打赏
  • 举报
回复
如果你打开string这个头文件,那么应该能总结出理由的,如果C++ Primer你认真看了关于函树重载解析的章节
hpj2000 2008-05-31
  • 打赏
  • 举报
回复
要细心咯~
五号智能 2008-05-31
  • 打赏
  • 举报
回复
啊!!!!!!!!!!!!
jy01807853 2008-05-31
  • 打赏
  • 举报
回复

//帮你改好了GCC上编译通过
#include <iostream>
using namespace std;//引用空间名就不用后面的,std::cout
struct abc
{
int a;
std::string b;
double c;
friend std::ostream& operator << (std::ostream &out,abc &hot)
{
out << hot.a << hot.b << hot.c;

return out;
}
};//这点问题
int main()
{
abc a;
a.a=3;
a.b="none";
a.c=3.204;
cout <<a<<endl;
system("pause");
return 0;
}

i244782405 2008-05-31
  • 打赏
  • 举报
回复
好象是要在头文件下加一句:
using manespace std;吧
std::cout <<a<<endl;
这个是什么哟 我从来没这样用过呢 还请楼下的指教下

64,635

社区成员

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

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