本文介绍了如何制作与指针一起使用的重载运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在主题中,我需要使用指针的运算符,因此我不必调用 *a>*b
而是 a>b
.例如我的操作符 <<
与指针一起工作:
As in the subject I need operator which will work with pointers so I do not have to call *a>*b
but a>b
.For example my operator <<
works with the pointers ok:
friend ostream& operator<< (ostream &wyjscie, Para const* ex){
wyjscie << "(" << ex->wrt << ", " << ex->liczbaWystapien <<")"<< endl;
return wyjscie;
}
但是这个给了我一个错误:
but this one give me an error:
friend bool operator> (Para const *p1, Para const *p2){
return p1->wrt > p2->wrt;
}
Error 1 error C2803: 'operator >' must have at least one formal parameter of class type
推荐答案
你的重载 <<之所以有效,是因为它是在 ostream 对象上调用的 (ostream.operator<<()).
Your overloaded << works because it is being called on an ostream object (ostream.operator<<()).
运算符
The pointer overload of operator < does not work because a pointer is not a class so the following is meaningless: (const Para*).operator<().
这篇关于如何制作与指针一起使用的重载运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!