gpt4 book ai didi

c++ - 运营商_surrogate_func : no matching overload found and 2 other errors

转载 作者:行者123 更新时间:2023-11-27 23:37:00 24 4
gpt4 key购买 nike

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

NEW_BUS - 通过获取公交车的编号、停靠站数量和停靠站列表在列表中添加新公交车。ALL_BUSES - 按字典顺序显示所有总线(按名称)STOPS_FOR_BUS - 显示特定公交车之后的所有站点。BUSES_FOR_STOP - 显示所有经过特定站点的公交车。

错误列表如下:1. operator _surrogate_func: 找不到匹配的重载2. 无法特化函数模板 'unknown-type std::less::operator ()(_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 正在返回对局部变量的引用。一般来说,这是一个坏主意,因为当您尝试时,内存可能根本无法再访问。只需从返回类型中删除“&”。 Further explanations .

关于c++ - 运营商_surrogate_func : no matching overload found and 2 other errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58547539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com