首页
社区
课程
招聘
[求助]C++重载操作符问题
发表于: 2012-9-13 10:49 3304

[求助]C++重载操作符问题

2012-9-13 10:49
3304

#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) 也不会出错..这是为啥呢?

[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 306
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
Student s(); 和  Student s; 是不同的 前者初始化并调用构造函数
2012-9-13 12:43
0
游客
登录 | 注册 方可回帖
返回
//