【发布时间】:2014-12-19 04:30:20
【问题描述】:
我有一个指向vector<object> 的向量指针,所以
const std::vector<object> vecPtr* = &vec;
现在我想以这种方式填写std::multimap<std::string, object*> dataMap;,其中key 是object.name,value 是pointer to an object。
我试过了
for(std::vector<object>::const_iterator it = data->cbegin(); it != data->cend(); ++it){
dataMap.insert(std::pair<std::string, object*>(it->name, &it));
}
但我得到一个错误。
error: no matching function for call to 'std::pair<std::basic_string<char>, object*>::pair(const string&, std::vector<object>::const_iterator*)'
dataMap.insert(std::pair<std::string, object*>(it->name, &it));
^
我做错了什么?
我知道指针会让我的生活变得复杂,但我想避免复制对象
【问题讨论】:
-
你应该确切地提到你得到的错误是什么。
-
这看起来很可疑。您将指针传递给迭代器。 'dataMap.insert(std::pair<:string object>(it->name, &it))*
-
你实际上是把迭代器的地址加到映射中,而不是指向对象的指针。
-
@Oncaphillis 所以我应该在末尾添加一个 * 吗?
-
vecPtr没有声明为指针,无论如何它似乎与问题的其余部分没有任何关系。