首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

返回一个引用如何不能行

2012-04-01 
返回一个引用怎么不能行?#include stdlib.h#include iostreamusing namespace stdclass a{private:in

返回一个引用怎么不能行?
#include <stdlib.h>
#include <iostream>
using namespace std;

class a
{
private:
int c;
public:
a (int f)
{
c=f;
}
ostream & friend operator <<(ostream &os, a &s)
{
os<<s.c;
return os;
}
};

void main()
{

a d(5);
cout << d <<endl;

}





这个我是看见书上写的 应该没错呀
ostream & friend operator <<(ostream &os, a &s)
{
os<<s.c;
return os;
}

[解决办法]
friend首先应该放在前面
其次:friend函数不应该在类内实现,而应该在类外实现

class a 

private: 
int c; 
public: 
a (int f) 

c=f; 
}

//friend 在前面
friend ostream & operator <<(ostream &os, a &s);
}; 
实现放在cpp文件中
ostream & operator <<(ostream &os, a &s) //<<英文状态下输入 { 
os <<s.c; 
return os; 

热点排行