结构体中的运算符重载在头文件中编译不通过在main.c中可以
#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