65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
void fun() const
{
cout<<"hello"<<endl;
}
int main(void)
{
fun();
return 0;
}
别说友元了,就上面这代码,都编译不过去!
non-member function cannot have `const' method qualifier#include <iostream>
#include <string>
using namespace std;
class Teacher;
class Student
{
int num;
string name;
string sex;
public:
void input();
void display();
Student operator=(Teacher &T) const ; //这里用到了const
};
class Teacher
{
int num;
string name;
string sex;
public:
void input();
void display();
friend Student Student::operator=(Teacher &T) const; //这里用到了const
};
void Teacher::input()
{
cin>>num>>name>>sex;
}
void Student::input()
{
cin>>num>>name>>sex;
}
void Teacher::display()
{
cout <<num <<endl;
cout <<name <<endl;
cout <<sex <<endl;
}
void Student::display()
{
cout <<num <<endl;
cout <<name <<endl;
cout <<sex <<endl;
}
Student Student::operator=(Teacher &T) const //这里用到了const
{
Student S;
S.name=T.name;
S.num=T.num;
S.sex=T.sex;
return S;
}
int main()
{
Teacher t;
t.input();
t.display();
Student s;
s.input();
s.display();
s=t;
s.display();
return 0;
}