我有:
“对象.h”
namespace objectNS
{
class object
{
public:
int m_number;
bool operator== (const object& object) const;
bool operator< (const object& object) const;
};
class compare
{
public:
bool operator ()(const object*, const object*) const;
};
}
“对象.cpp”
#include "object.h"
typedef objectNS::object OBJECT;
bool OBJECT::operator== (const object &object) const
{
return this->m_number == object.m_number;
}
bool OBJECT::operator< (const object &object) const
{
return this->m_number < object.m_number;
}
bool objectNS::compare::operator() (const object* obj1, const object* obj2) const
{
return obj1->m_number > obj2->m_number;
}
我有一些适用于对象的功能,
void f(void)
{
set<Compound_object*> detectedObjects;
set<Compound_object*> deleteFromTrackedObjects;
set_difference(detectedObjects.begin(), detectedObjects.end(), this->m_trackedObjects.begin(), this->m_trackedObjects.end(), inserter(addToTrackedObjects, addToTrackedObjects.end()));
}
问题描述:
我实现了operator<
and ,operator==
通过它来执行两个实例的比较,m_number
但是当集合detectedObjects
和trackedObjects
包含相同m_number
的元素时,std::set_difference
返回所有元素而不是预期的空结果集。
我什至尝试将函数对象compare
作为模板参数提供给集合,但结果我得到了很多编译器错误=
并且!=
没有正确定义。
我想知道是什么问题?