如何重载我的运算符,以便我可以比较两个顶点指针?在C++中重载比较运算符
class graph
{
private:
class vertex;
class edge;
class vertex
{
public:
int index;
int estDistanceToGoal;
int traveledDis;
string location;
这里是一个当我重载>运算符的例子。 问题是当我有一个顶点对象与顶点指针进行比较时触发器的这种方法。
bool operator> (vertex* v)
{
cout<<"we did this"<<endl;
return (this->weight > v->weight);
}
这是我用来测试它的一个例子。如果我有一个顶点对象和一个顶点指针进行比较,它只会打印“我们做了这个”。那么我怎样才能做到这一点,所以我比较了两个顶点指针?你能尊重“这个”吗?
void test()
{
vertex*a = new vertex("wj",5);
vertex*v = new vertex("something",2);
if(a == v)
{
cout<<"what"<<endl;
}
else
{
cout<<"k"<<endl;
}
}
您应该比较顶点本身,即'if(* a <* v)',并定义比较对象以获取顶点参数。 – Barmar
您不希望能够通过比较两个“int”值来比较指向它们的指针,为什么您会用'vertex'做不同的操作? – Barmar