没有重载函数的实例 std::unordermap.insert
no instance of overloaded function std::unordermap.insert
我声明了一个streamclass,但是在定义它的成员函数的时候,想往private成员里面插入东西的时候总是报错unordered_map,请问如何解决这个?
stream.h
#include <unordered_map>
class stream
{
private:
std::unordered_map<size_t, std::string> word_count;
public:
void reassemblestream() const;
};
stream.cpp
#include"stream.h"
using namespace std;
void stream::reassemblestream() const
{
static size_t a = 1;
string fe = "dfi";
word_count.insert({ 1,"dfs" });
word_count.insert(make_pair(a, fe));
}
word_count.insert
函数的 stream.cpp 发生错误。
这是错误信息:
E1087 There is no overloaded function "std::unordered_map<_Kty, _Ty,
_Hasher, _Keyeq, _Alloc>::insert [where _Kty=size_t, _Ty=std::string, _Hasher=std:: hash<size_t>, _Keyeq=std::equal_to<size_t>, _Alloc=std::allocator<std::pair<const size_t, std::string>>]" instance (object contains type qualifiers that prevent matching)
我用的是visual studio2019.
如果使用 clang++
构建程序,我们会得到更清晰的错误消息:
source>:17:16: error: no matching member function for call to 'insert'
word_count.insert({ 1,"dfs" });
~~~~~~~~~~~^~~~~~
/opt/compiler-explorer/gcc-11.1.0/lib/gcc/x86_64-linux-gnu/11.1.0/../../../../include/c++/11.1.0/bits/unordered_map.h:557:7: note: candidate function not viable: 'this' argument has type 'const std::unordered_map<size_t, std::string>' (aka 'const unordered_map<unsigned long, basic_string<char>>'), but method is not marked const
insert(value_type&& __x)
只需去掉const
限定符,因为你是通过调用insert
函数来修改数据成员,函数reassemblestream
不能被标记为const(相关question 对于 const 成员函数):
#include <unordered_map>
#include <string>
class stream
{
private:
std::unordered_map<size_t, std::string> word_count;
public:
void reassemblestream();
};
void stream::reassemblestream()
{
static size_t a = 1;
std::string fe = "dfi";
word_count.insert({ 1,"dfs" });
word_count.insert(make_pair(a, fe));
}
我声明了一个streamclass,但是在定义它的成员函数的时候,想往private成员里面插入东西的时候总是报错unordered_map,请问如何解决这个? stream.h
#include <unordered_map>
class stream
{
private:
std::unordered_map<size_t, std::string> word_count;
public:
void reassemblestream() const;
};
stream.cpp
#include"stream.h"
using namespace std;
void stream::reassemblestream() const
{
static size_t a = 1;
string fe = "dfi";
word_count.insert({ 1,"dfs" });
word_count.insert(make_pair(a, fe));
}
word_count.insert
函数的 stream.cpp 发生错误。
这是错误信息:
E1087 There is no overloaded function "std::unordered_map<_Kty, _Ty, _Hasher, _Keyeq, _Alloc>::insert [where _Kty=size_t, _Ty=std::string, _Hasher=std:: hash<size_t>, _Keyeq=std::equal_to<size_t>, _Alloc=std::allocator<std::pair<const size_t, std::string>>]" instance (object contains type qualifiers that prevent matching)
我用的是visual studio2019.
如果使用 clang++
构建程序,我们会得到更清晰的错误消息:
source>:17:16: error: no matching member function for call to 'insert'
word_count.insert({ 1,"dfs" });
~~~~~~~~~~~^~~~~~
/opt/compiler-explorer/gcc-11.1.0/lib/gcc/x86_64-linux-gnu/11.1.0/../../../../include/c++/11.1.0/bits/unordered_map.h:557:7: note: candidate function not viable: 'this' argument has type 'const std::unordered_map<size_t, std::string>' (aka 'const unordered_map<unsigned long, basic_string<char>>'), but method is not marked const
insert(value_type&& __x)
只需去掉const
限定符,因为你是通过调用insert
函数来修改数据成员,函数reassemblestream
不能被标记为const(相关question 对于 const 成员函数):
#include <unordered_map>
#include <string>
class stream
{
private:
std::unordered_map<size_t, std::string> word_count;
public:
void reassemblestream();
};
void stream::reassemblestream()
{
static size_t a = 1;
std::string fe = "dfi";
word_count.insert({ 1,"dfs" });
word_count.insert(make_pair(a, fe));
}