此示例代码无法编译
This sample code won't compile
我无法理解在cygwin shell中编译此代码时收到的错误消息。消息很长,但在这个1000行错误的中间,它说:
没有对运营商<
这是什么意思?这是我的代码:
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;
struct Grade{
string id;
int score;
bool operator() (Grade& a, Grade& b){
return a.id < b.id;
}
};
int main()
{
Grade g;
set<Grade> gs;
g.id = "ABC123";
g.score = 99;
gs.insert(g);
g.id = "BCD321";
g.score = 96;
gs.insert(g);
for(auto it : gs)
cout << it.id << "," << it.score;
return 0;
}
集合需要其元素类型来定义小于运算符。看见http://www.cplusplus.com/reference/set/set/?kw=set
你可以这样定义它(在等级定义之后(:
bool operator< (const Grade& a, const Grade& b){
return a.id < b.id;
}
std::set
按排序顺序存储其元素,这要求其元素类型为其定义operator <
。在这种情况下,您需要为Grade
类型定义operator <
。
bool operator < (const Grade& grade1, const Grade& grade2)
{
return grade1.score < grade2.score; // or other method of determining
// if a grade is less than another
}
或者,如果您想在结构本身中定义它:
bool operator < ( const Grade& grade2) const
{
return score < grade2.score; // or other method of determining
// if a grade is less than another
}
如果为Grade
重载operator<()
函数,则可以创建std::set<Grade>
。可以使用成员函数或非成员函数来定义函数。
无论采用哪种方法,都必须定义函数,以便LHS和RHS都可以是const
对象。
成员功能方法:
struct Grade{
string id;
int score;
bool operator<(Grade const& rhs) const
{
return this->id < rhs.id;
}
};
非成员函数方法:
struct Grade{
string id;
int score;
};
bool operator<(Grade const& lhs, Grade const& rhs)
{
return lhs.id < rhs.id;
}
感谢大家的帮助,我看到了很多解决方案,这是我的代码,它按升序对ID进行排序
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;
struct Grade{
string id;
int score;
bool operator< (const Grade& g) const {
return this->id < g.id;
}
};
int main()
{
Grade g;
set<Grade> gs;
g.id = "ABC123";
g.score = 99;
gs.insert(g);
g.id = "BCD321";
g.score = 96;
gs.insert(g);
for(auto it : gs)
cout << it.id << "," << it.score << endl;;
return 0;
这是正确编译的更新代码。std::set
的"operator<"问题已解决:
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;
struct Grade{
string id;
int score;
bool operator<(const Grade& that) const
{
return this->score < that.score;
}
bool operator() (Grade& a, Grade& b){
return a.id < b.id;
}
};
int main() {
Grade g;
set<Grade> gs;
g.id = "ABC123";
g.score = 99;
gs.insert(g);
g.id = "BCD321";
g.score = 96;
gs.insert(g);
for(auto it : gs)
cout << it.id << "," << it.score;
return 0;
}
相关文章:
- 在Linux for Windows上编译C++代码时出错
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在程序中编写脚本来编写和编译代码
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 在macos上编译代码的未解析符号
- 使用个人C++库编译代码时,与头文件一起使用时会中断
- 使用指针编译代码后,.cpp文件将变为随机字符
- VS Express 无法正确编译代码(?
- 如何在Ubuntu中使用Visual Studio代码编译C++代码
- 使用 Android NDK 使用 clang++ 编译C++代码时对"_Unwind_Resume"的未定义引用
- 无法使用两个包装不同下一层的ssl_stream编译代码
- 编译代码时"[Warning] extra tokens at end of"
- 无法编译代码,因为它已在 C++11 中弃用
- 如何在 Azure 应用服务中使用 cl.exe 编译 C++ 代码并生成可执行文件
- 如何调试编译 c++ 代码的 bazel?
- 为什么在使用转换构造函数编译代码时需要 const 复制构造函数?
- 编译 C++ 代码后尺寸较大
- 使用用 C++ 和 DDS 编写的 CMAKE 编译代码
- 编译代码时如何不制作二进制文件?
- 如何使用所有其他文件信息来编译代码,例如HAAR级联训练的权重的XML文件