-
-
[求助]C++重载操作符问题
-
发表于:
2012-9-13 10:49
3415
-
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
void say(){
cout<<"hello i'm student"<<endl;
}
bool operator==(const Student&){
return 1;
}
protected:
private:
int i;
};
main(){
Student s();
Student s1();
bool w=(s==s1);
return 0;
}
以上代码执行出现异常 unresolved external symbol "class Student __cdecl s(void)" (?s@@YA?AVStudent@@XZ)
将 bool w=(s==s1); 改成s==s1就没有错误
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
Student(int i){
}
void say(){
cout<<"hello i'm student"<<endl;
}
bool operator==(const Student&){
return 1;
}
protected:
private:
int i;
};
main(){
Student s(1);
Student s1(2);
bool w=(s==s1);
return 0;
}
必须加上一个有参数的构造才可以正常运行.声明不加()写成 Student s;Student s1
bool w=(s==s1) 也不会出错..这是为啥呢?
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!