关于重载<<的问题~
#include<iostream>#include<cstring>
using namespace std;
class Stu{
char name[20];
float score;
public:
Stu(char *p,float s){
strcpy(name,p);
score=s;
}
Stu(){
score=0;
strcpy(name,"no name");
}
void set(char *p,float s){
strcpy(name,p);
score=s;
}
friend ostream & operator << (ostream &os, Stu&x) ;
};
ostream & operator << (ostream &os, Stu &x){
os << "name" << x.name << "\tscore" << x.score <<endl;
return os;
}
int main(){
Stu x("Xuu",93.5);
cout << x;
return 0;
}
为什么总是出错啊~高手帮忙~ 友元的问题,VC++6.0不支持友元,换编译器 #include<iostream>
#include<cstring>
using namespace std;
namespace demo{
class Stu{
char name[20];
float score;
public:
Stu(char *p,float s){
strcpy(name,p);
score=s;
}
Stu(){
score=0;
strcpy(name,"no name");
}
void set(char *p,float s){
strcpy(name,p);
score=s;
}
friend ostream & operator << (ostream &os, Stu &x) ;
};
ostream & operator << (ostream &os,Stu &x){
os << "name:" << x.name << "\tscore:" << x.score <<endl;
return os;
}
}
int main(){
demo::Stu x("Xuu",93.5);
cout << x;
return 0;
}
回复 3# 的帖子
厉害啊!不过请教下这个是为什么呢? 答案是2楼说的原因,要用命名空间隔开空间污染..用vs系列不存在这个问题..回复 5# 的帖子
谢谢啊!!不过为什么会空间污染呢? 因为std空间里面也涉及这个<<重载,编译器解析的时候会出现问题..你看看书吧...呵呵回复 7# 的帖子
恩 。。不过不知道看什么书啊。。我都是自己看网上下的教程的,不知道哪本学C++比较好啊 c++ primer plus起步用书..c++ primer比较适合有点基础看回复 9# 的帖子
英文的吗?回复 9# 的帖子
诶~找到中文版的了~正在下~呵呵页:
[1]
