operator重载!!
来源:百度知道 编辑:UC知道 时间:2024/05/31 10:30:01
#include<iostream>
using namespace std;
class stu_age
{
public:
stu_age operator+=(const stu_age&);
public:
friend istream& operator>>(istream&, stu_age&);
stu_age():
year(1980),mouth(1),day(1){}
// stu_age(istream &is){is >> *this;}
void output() {cout<<year<<"year"<<mouth<<"mouth"<<day<<"day"<<'\n';}
private:
int year;
int mouth;
int day;
};
stu_age operator+(const stu_age&,const stu_age&);
inline istream&
operator>>(istream& in,stu_age& s)
{
in >>s.year>>s.mouth>>s.day;
return in;
}
stu_age n;
编译过程中总是显示不能进入private成员??如何改啊??
using namespace std;
class stu_age
{
public:
stu_age operator+=(const stu_age&);
public:
friend istream& operator>>(istream&, stu_age&);
stu_age():
year(1980),mouth(1),day(1){}
// stu_age(istream &is){is >> *this;}
void output() {cout<<year<<"year"<<mouth<<"mouth"<<day<<"day"<<'\n';}
private:
int year;
int mouth;
int day;
};
stu_age operator+(const stu_age&,const stu_age&);
inline istream&
operator>>(istream& in,stu_age& s)
{
in >>s.year>>s.mouth>>s.day;
return in;
}
stu_age n;
编译过程中总是显示不能进入private成员??如何改啊??
这是vc6 的问题,要在class里面定义才行:
#include<iostream>
using namespace std;
class stu_age
{
public:
stu_age operator+=(const stu_age&);
public:
friend istream;
friend istream& operator >>(istream& in, stu_age &s)
{
in >> s.year >> s.mouth >> s.day;
return in;
}
stu_age():
year(1980),mouth(1),day(1){}
// stu_age(istream &is){is >> *this;}
void output() {cout<<year<<"year"<<mouth<<"mouth"<<day<<"day"<<'\n';}
private:
int year;
int mouth;
int day;
};
stu_age operator+(const stu_age&,const stu_age&);
stu_age n;
private:
int year;
int mouth;
int day;
这个地方的private改成public就可以了,不过这样不好
因为你的
int year;
int mouth;
int day;
权限是private;所以你不能直接使用成员,如果你想直接使用
C++模板问题:重载operator<<
C++重载operator new/delete
重载operator new/delete的问题
请问 operator 在C++是是什么观键字呀?除了重载以外
运算符重载的问题:int operator+();与int &operator+();有什么区别?
C++运算符重载:complex operator+(complex c2); //+重载为成员函数;;C2参数是哪里传来的
为什么C++操作符<<重载老是提示:operator <<' is ambiguous
C++中定义一个字符串类string,并实现减运算符重载operator-
C++ 写了复制构造函数是不是不用重载operator =
重载'='时报错:error C2801: 'operator =' must be a <Unknown> member是怎么回事?