operator_surrogate_func:未找到匹配的重载和其他2个错误

operator _surrogate_func: no matching overload found and 2 other errors

本文关键字:重载 2个 错误 其他 func surrogate operator      更新时间:2023-10-16

我写了一段代码,创建了一个可以修改和管理的总线列表。整个管理过程是通过在控制台中写入字符串来进行的。在运行代码后,我收到了3个错误,因此我所理解的没有一个可以修复。代码是这样计划的:

NEW_BUS-通过获取新巴士的编号、停靠站数量和停靠站列表,将其添加到列表中。ALL_BUSES-按字典顺序显示所有总线(按名称(STOPS_FOR_BUS-显示所有站点特定的总线跟随。BUSES_FOR_STOP-Diploy所有经过特定站点的公交车。

以下是错误列表:1.operator_surrogate_func:未找到匹配的重载2.无法专门化函数模板的未知类型std::less::运算符(((_Ty 1&,_Ty2&&(3.非法表达

所有错误都来自xutility文件的617行。

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <tuple>
#include <iterator>
using namespace std;
class Bus {
public:
int StopsAmount;
string BusNumber;
vector<string> Stops;
Bus(tuple<string, int, vector<string>> BusParams) {
BusNumber = get<0>(BusParams);
StopsAmount = get<1>(BusParams);
Stops = get<2>(BusParams);
}
void ShowStops() {
cout << BusNumber << ": ";
for (int i = 0; i < StopsAmount; i++) cout << Stops[i] << " ";
cout << "n";
}
bool FindStop(string Stop) {
for (int i = 0; i < StopsAmount; i++) {
if (Stops[i] == Stop) {
return true;
}
}
return false;
}
};   
class BusTraffic {
public:
BusTraffic() {
while (true) {
string Request;
cin >> Request;
switch (Request[0]) {
case 'N': NEW_BUS(Request.substr(8, Request.length() - 8))      ;
break;
case 'B': BUSES_FOR_STOP(Request.substr(15, Request.length() - 15));
break;
case 'S': STOPS_FOR_BUS(Request.substr(14, Request.length() - 14));
break;
case 'A': ALL_BUSES();
break;
}
}
}
private:
list<Bus> BusList;
void NEW_BUS(string Request) {
BusList.push_back(Bus::Bus(SplitString(Request)));
}
void BUSES_FOR_STOP(string Stop) {
cout << Stop << ": ";
for (list<Bus>::iterator It = BusList.begin(); It != BusList.end(); It++) {
if (It->FindStop(Stop)) {
cout << It->BusNumber << " ";
}
}
cout << endl;
}
void STOPS_FOR_BUS(string Name) {
cout << Name << ": ";
for (list<Bus>::iterator It = BusList.begin(); It != BusList.end(); It++) {
if (It->BusNumber == Name) {
It->ShowStops();
}
}
}
void ALL_BUSES() {
if (BusList.size() > 0) {
BusList.sort();
for (list<Bus>::iterator It = BusList.begin(); It != BusList.end(); It++) {
cout << It->BusNumber << ": ";
It->ShowStops();
}
}
else {
cout << "No buses" << endl;
}
}
// Converting string to information about bus
tuple<string, int, vector<string>> &SplitString(string str) {
tuple<string, int, vector<string>> BusParams;
string Word = "";
int WordNum = 0;
for (auto Letter : str) {
if (Letter == ' ') {
if (WordNum == 0) get<0>(BusParams) = Word; 
if (WordNum == 1) get<1>(BusParams) = stoi(Word);
if (WordNum == 2) get<2>(BusParams).push_back(Word);
Word = "";
WordNum++;
}
else {
Word = Word + Letter;
}
}
get<2>(BusParams).push_back(Word);
return BusParams;
}
};
int main() {
BusTraffic TestTraffic;
return 0;
}

不幸的是,你完全正确。这里的错误消息太可怕了。只是从经验中,你学会了将其解释为一个缺失的小于运算符。

因此,

bool operator< (const Bus& lhs, const Bus& rhs) {
// however you want to sort them...
return (lhs.BusNumber < rhs.BusNumber);
}

另一个小错误在NEW_BUS中:在C++中,您不需要指定构造函数名称。所以它不是Bus::Bus,它只是Bus

最后但并非最不重要的是,您的SplitString将返回对局部变量的引用。一般来说,这是个坏主意,因为当你尝试访问时,内存可能根本无法访问。只需从返回类型中删除"&"即可。进一步解释。

您正在尝试对总线列表进行排序。根据它的声音,小于操作员没有过载。这意味着C++不知道如何比较总线类型的对象。将此添加到您的Bus类中,它应该可以工作。如果另一条总线小于当前总线,则小于运算符的过载将返回true。

bool operator < (const Bus& otherBus) const {
if(otherBus.StopsAmount < this.StopsAmount) {
return true;
}
return false;
}

提供更多详细信息的有用链接:http://fusharblog.com/3-ways-to-define-comparison-functions-in-cpp/

https://www.tutorialspoint.com/cplusplus/relational_operators_overloading.htm