使用向量和结构在C 中遇到错误
Getting an error in C++ using vector and struct
此代码中的错误是什么:
#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/includs/c /4.8.2/bits/stl_algo.h:166:17:错误:"操作员=="无匹配(操作数类型为'symtab'和'const std :: basic_string'( 如果(*__ first == __val(
您正在尝试在std::vector<symtab>
中找到std::string
,"ADD"
。当然这不起作用。
您需要的是std::find_if
。
auto it = std::find_if(symtab_details.begin(),
symtab_details.end(),
[&s](symtab const& item) { return item.name == s; });
return (it != symtab_details.end());
代码正在搜索与类型std::string
对象的类型symtab
对象。因此,您必须提供一个比较操作员,以确定特定的symtab
对象是否等于特定的std::string
对象。您需要
bool operator==(const symtab&, const std::string&);
如果您仔细阅读错误消息,那就是它告诉您的。
相关文章:
- 我在 C++ 代码中遇到错误警告:控制到达非空函数 [-Wreturn 类型] 的末尾
- 我在 ndk 中构建文件时遇到错误 android.mk
- 我们在批处理模式下使用 G++ 时遇到错误
- 在visual Studio中构建代码时,我遇到错误,.h文件丢失
- 学习C++并在早期示例中遇到错误(在非静态数据成员之前需要构造函数)
- 可中断线程类 C++11 - 遇到错误?
- 在尝试在同一类内部进行课程矢量时,我会遇到错误
- 使用向量和结构在C 中遇到错误
- 我正在尝试安装游戏引擎,但遇到错误
- 使用 bazel 从源代码构建张量流服务遇到错误:C++规则'@org_tensorflow//…'编译失败(出口 4)
- 为什么尝试这样的类代码时会遇到错误?我初学者对不起
- 当 Mingw 为 C++ 编译 OpenCV 时,我遇到错误?
- 遇到错误试图在功能型参数中使用void函数的错误
- 尝试使用Boost和Ncurses库进行编译程序时会遇到错误
- 我在使用C 的Hangman游戏中遇到错误
- 为什么我在加载共享库时遇到错误
- 我在 c++ 中遇到错误'PTHREAD_START_ROUTINE'未在此范围内声明
- 学习如何在遇到错误'variable not declared in scope'的 c++ 中使用指针
- 如果遇到错误,如何重复代码
- 当我尝试将UDP套接字绑定到带有SFML的端口时,为什么会遇到错误