结构体中的运算符重载在头文件中编译不通过在main.c中可以

JLQ1991 2018-03-12 04:10:58
#include <iostream>
#include <algorithm>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;
typedef struct _Student{
std::string name;
float score;
bool operator<(const Student &otherScore){

return score< otherScore.score;//return turn or false

}
friend std::ostream &operator<<(std::ostream &output, const Student &otherScore){
output << "Student:" << otherScore.score << " " << otherScore.score << endl;
return output;
}
}Student;

结构体中的运算符重载在头文件中出错
错误 2 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int c:\users\jlq\desktop\task\test\algorithmtest\algorithmtest\sortdatahelper.h 15 1 AlgorithmTest
错误 3 error C2143: 语法错误 : 缺少“,”(在“&”的前面) c:\users\jlq\desktop\task\test\algorithmtest\algorithmtest\sortdatahelper.h 15 1 AlgorithmTest
错误 4 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int c:\users\jlq\desktop\task\test\algorithmtest\algorithmtest\sortdatahelper.h 20 1 AlgorithmTest
错误 5 error C2143: 语法错误 : 缺少“,”(在“&”的前面) c:\users\jlq\desktop\task\test\algorithmtest\algorithmtest\sortdatahelper.h 20 1 AlgorithmTest
错误 6 error C2065: “otherScore”: 未声明的标识符 c:\users\jlq\desktop\task\test\algorithmtest\algorithmtest\sortdatahelper.h 17 1 AlgorithmTest
错误 7 error C2228: “.score”的左边必须有类/结构/联合 c:\users\jlq\desktop\task\test\algorithmtest\algorithmtest\sortdatahelper.h 17 1 AlgorithmTest
错误 8 error C2065: “otherScore”: 未声明的标识符 c:\users\jlq\desktop\task\test\algorithmtest\algorithmtest\sortdatahelper.h 21 1 AlgorithmTest
错误 9 error C2228: “.score”的左边必须有类/结构/联合 c:\users\jlq\desktop\task\test\algorithmtest\algorithmtest\sortdatahelper.h 21 1 AlgorithmTest
错误 10 error C2679: 二进制“<<”: 没有找到接受“Student”类型的右操作数的运算符(或没有可接受的转换) c:\users\jlq\desktop\task\test\algorithmtest\main.cpp 18 1 AlgorithmTest
...全文
318 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
JLQ1991 2018-03-12
  • 打赏
  • 举报
回复
发现如果不定义成结构体而变成类就没有问题了 这是什么原因呢 C++不能再结构体内进行函数从在么?
JLQ1991 2018-03-12
  • 打赏
  • 举报
回复
在每个运算符重载那一行都显示: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
JLQ1991 2018-03-12
  • 打赏
  • 举报
回复
将以上代码挪到main()之前就好了 这是为什么呢?

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧