"Item"이라는 클래스가 있는데 항목 집합에 항목을 삽입하려고합니다.컴파일러가 왜 std :: set <Item>에 항목을 삽입 할 수 없다고 말합니까?
std::set<Item>::iterator it;
_items.insert(it, newItem);
하지만 내게는이 연산자를 구현했다하더라도이 이상한 오류
Error 1 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Item' (or there is no acceptable conversion)
을 제공합니다.
bool Item::operator<(Item& other)
{
return _serialNumber < other._serialNumber;
}
이 함수에서이 연산자가 필요한 이유가 표시되지 않습니다.
누군가 문제가 무엇인지 알고 있습니까?
미리 감사드립니다.
bool Item::operator<(const Item& other) const
^^^^^ ^^^^^
{
return _serialNumber < other._serialNumber;
}
당신이 확인해야하기 때문이다
힌트에 오류가 있습니다 : 'const Item', 연산자는 const ref로 다른 것을 취해야합니다. (당신은 연산자도 const로 만들어야한다.) – Borgleader
bool 구현하기 :: operator <(const Item & other) – PaulD
it itator는 아무것도 참조하지 않지만'insert' 호출에서 사용한다. – PaulMcKenzie