【发布时间】:2018-09-10 13:38:29
【问题描述】:
这段代码有什么错误:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct symtab{
string name;
string location;
};
vector<symtab> symtab_details;
bool search_symtab(string s){
if (find (symtab_details.begin(), symtab_details.end(), s)!=symtab_details.end()) return true;
return false;
}
int main() {
bool get = search_symtab("ADD");
return 0;
}
我收到以下错误:
usr/include/c++/4.8.2/bits/stl_algo.h:166:17: 错误:'operator==' 不匹配(操作数类型是'symtab' 和'const std::basic_string') if (*__first == __val)
【问题讨论】:
-
find函数试图在你的向量中找到一个symtab结构。了解 lambda expressions 和std::find_if了解解决问题的一种方法。 -
在符号表类中重载 operator== 以便可以将其与 const std::string & 进行比较
-
错误很明显。如果您不明白为什么这是一个错误,请在提出有关它的问题之前阅读有关运算符重载的更多信息,因为它是该语言的一个非常基本的特性。
标签: c++ algorithm vector struct