C++ C++;重载运算符链接排序列表ADT
我一直在试图使相等(=)和小于( //我不确定 目的地城市是我想要的 应该写 你不应该:-)。如果要在某个类上定义C++ C++;重载运算符链接排序列表ADT,c++,operator-overloading,adt,C++,Operator Overloading,Adt,我一直在试图使相等(=)和小于( //我不确定 目的地城市是我想要的 应该写 你不应该:-)。如果要在某个类上定义运算符
如果您使用的是运算符
DestinationCity,other.DestinationCity)) ... }
std::string
,请不要与strcmp
进行比较=
和如果要使“struct”不包含成员,可以将二进制运算符定义为非成员:
bool operator <(const flightRec& lhs, const flightRec& rhs)
{
if (strcmp(lhs.DestinationCity, rhs.DestinationCity) < 0)
return true;
else
return false;
}
bool操作符flightRec是一个结构,而不是一个类。我试图更改您添加的内容,但它给了我:sortedListClass.cpp:185:50:error:no'bool flightRec::operator@AthetiusC++中的结构和类完全是一样的。唯一的区别是,如果使用class
,基类和成员默认为private
,如果使用struct
,基类和成员默认为public
。我的代码只是一个示例。您需要移动operator@Athetius:您的老师可能正在使用(和教授)一种风格,即您对不打算提供任何重要成员函数、任何私有数据成员或任何基类的类使用struct
关键字,对其他所有类使用class
。但是风格是你选择如何使用语言,而不是语言的实际内容。。。
bool flightRec::operator< (const flightRec& other) {
// compare this and other
if (strcmp(this->DestinationCity, other.DestinationCity))
...
}
bool operator <(const flightRec& lhs, const flightRec& rhs)
{
if (strcmp(lhs.DestinationCity, rhs.DestinationCity) < 0)
return true;
else
return false;
}