Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么编译器说我可以';t将项目插入标准::集合<;项目>;?_C++_Stl_Insert_Set_Operator Keyword - Fatal编程技术网

C++ 为什么编译器说我可以';t将项目插入标准::集合<;项目>;?

C++ 为什么编译器说我可以';t将项目插入标准::集合<;项目>;?,c++,stl,insert,set,operator-keyword,C++,Stl,Insert,Set,Operator Keyword,我有一个名为“Item”的类,我正在尝试将一个项插入到一组项中 std::set<Item>::iterator it; _items.insert(it, newItem); std::set::迭代器; _项目。插入(it,新项目); 但它给了我一个奇怪的错误 Error 1 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Ite

我有一个名为“Item”的类,我正在尝试将一个项插入到一组项中

std::set<Item>::iterator it;
_items.insert(it, newItem);
std::set::迭代器;
_项目。插入(it,新项目);
但它给了我一个奇怪的错误

Error   1   error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Item' (or there is no acceptable conversion)

Error 1 Error C2678:binary'您应该将定义更改为:-

bool Item::operator<( const Item& other)  const
                      ^^^^^               ^^^^^
{
    return _serialNumber < other._serialNumber;
}

bool Item::operator您应该将定义更改为:-

bool Item::operator<( const Item& other)  const
                      ^^^^^               ^^^^^
{
    return _serialNumber < other._serialNumber;
}
bool项::运算符
“我不明白为什么在这个函数中需要这个运算符。”

这是因为您需要确保,在
Item
类的rvalue实例上调用此运算符函数。这是一组
std::set
(以及其他容器类,其中
Item
应该用作键)

“我不明白为什么在这个函数中需要这个运算符。”


这是因为您需要确保对
类的右值实例调用此运算符函数。这是一组
std::set
(以及其他容器类,其中
项应用作键)。

错误中有提示:“const Item”,您的运算符应按const ref获取other。(您可能也应使运算符const)实现bool Item::operatorAlso,您的
it
迭代器没有引用任何内容,但是您在
insert
调用中使用它。@AlonPe'er这将是一个完全不同的问题(保罗的评论可能会涉及到这个问题)。建议你仔细阅读。永远不要改变发布问题的基本背景!你只是把所有已经发布的答案和评论在现在发布的文本中变得毫无意义。如果有不同的问题,发布一个新问题。@WhozCraig同意。回滚到您的版本。提示出现在错误中:“const Item”,您的运算符应该按const ref获取其他内容(您可能也应该使运算符const)实现bool Item::operator。因此,您的
it
迭代器没有引用任何内容,但是您在
insert
调用中使用它。@AlonPe'er这将是一个完全不同的问题(保罗的评论可能会涉及到这个问题)。建议你仔细阅读。永远不要改变发布问题的基本背景!你只是把所有已经发布的答案和评论在现在发布的文本中变得毫无意义。如果有不同的问题,发布一个新问题。@WhozCraig同意。已回滚到您的版本。谢谢,但现在它崩溃了…:(是时候调试它并在问题仍然存在时提出另一个问题:)谢谢,但现在它崩溃了…:(如果问题仍然存在,是时候调试它并提出另一个问题了:)