尝试使用从 ptr_map::find 返回的迭代器时出错
Error trying to use iterator returned from ptr_map::find
我在下面的代码上收到编译错误。错误是:
/ ... /main.cpp:21:22: error: no member named 'name' in 'boost::ptr_container_detail::ref_pair<std::__1::basic_string<char>, repo *const>'
std::cout << it->name;
~~ ^
我在这里做错了什么?我已经尝试了 it.name 和它>名称,但都不起作用。
问候,大卫。
#include <boost/ptr_container/ptr_map.hpp>
class repo {
public:
repo(const std::string & repo_name) : name(repo_name) {}
std::string name;
};
typedef boost::ptr_map<std::string, repo> repo_map;
repo_map repos;
int main() {
std::string repo_name("x");
repo_map::iterator it = repos.find(repo_name);
if (it == repos.end()) {
it = repos.insert(repo_name, new repo(repo_name)).first;
}
std::cout << it->name;
return 0;
}
因为它是一个 ptr_map,所以它返回pair<key, ptr_value>
。所以你需要:
it->second->name
我认为这将完成工作
它->second.name
相关文章:
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- C++ find() 在存储为变量时返回不同的值
- 在先前调用 string::find 后添加内存分配和内存集会导致它返回 npos.为什么?
- 在数组中查找元素,如果找到则返回Find,否则返回not Find
- find() 没有返回可以使用的 int 值,并且在编译之前给出错误
- std::string::find 为取消引用的迭代器和等效字符串文本返回不同的值
- 由 std::find() 返回的迭代器不可取消引用
- 为什么空 unordered_map.find 不返回 end()?
- C++ std::find() 寻址返回向量的类函数时的意外行为
- std::string::find 返回错误的答案
- std::string::find 总是返回 string::npos 偶数
- GetPrivateProfileStringA 返回"The system cannot find the file specified."即使文件存在
- String.find() 在 C++ 中找不到任何内容时不会返回 -1
- 为什么要对 map::find 应用不存在的键将返回一个C++中第一个值映射大小的迭代器
- std::map中的find()函数返回一个值,尽管没有有效的键
- std :: map find()返回错误
- 自定义类对象的向量的std::find的返回值
- 哪个元素将从 std::multimap::find 返回,同样,std::multiset::find
- 使用 string::find 和 string::substr 拆分字符串的函数返回错误的标记
- c++二进制搜索无论用户类型如何都会返回(find)