C++编译报此错误,有一种可能是你将对像添加到容器中但没有重载“<”运算符。参考下列代码重载一下就可以了(以set容器为例)。
类重载<运算符代码:
class Student
{
public:
std::string m_name;
bool operator < (const Student& other) const
{
return this->m_name < other.m_name;
}
};
测试工程代码:
#include <iostream>
#include <string>
#include <set>
namespace
{
class Student
{
public:
std::string m_name;
bool operator < (const Student& other) const
{
return this->m_name < other.m_name;
}
};
std::set<Student> stuSet;
}
int main()
{
stuSet.insert(Student());
return 0;
}