Linux 编译错误 - 无法将类型"字符串"转换为类型"字符串&&"
Linux compilation error - cannot convert type ‘string’ to type ‘string&&’
Linux编译错误-无法将类型"string"转换为类型"string&;',当插入到包含字符串的映射中时。在Windows上构建良好也是如此。我的用例:
void insertIf(std::string str1, int value, std::map<std::string,int> &myMap) const
{
if(value == 1)
{
myMap.insert(std::make_pair<std::string, int>(str1, value));
}
}
这是因为std::make_pair
将转发引用作为参数。当您想要显式指定模板参数时,应该将std::make_pair
与推导上下文和std::pair
的构造函数一起使用。
以下是您的选择:
make_pair:
void insertIf(std::string str1, int value, std::map<std::string,int>& myMap) {
if(value == 1 {
myMap.insert(std::make_pair(str1, value));
}
}
配对构造函数
void insertIf(std::string str1, int value, std::map<std::string,int>& myMap) {
if(value == 1 {
myMap.insert(std::pair<std::string, int>(str1, value));
}
}
更好,emplace
:
void insertIf(std::string str1, int value, std::map<std::string,int>& myMap) {
if(value == 1 {
myMap.emplace(str1, value);
}
}
如果你真的想将std::make_pair
与显式参数一起使用,你可以在模板参数中指定值类别,但我建议你不要这样做,这有点违背std::make_pair
:的全部目的
void insertIf(std::string str1, int value, std::map<std::string,int>& myMap) {
if(value == 1 {
myMap.insert(std::make_pair<std::string&, int&>(str1, value));
}
}
编辑:它在Windows上工作,因为您必须使用过时版本的VS,该版本还不支持转发引用。
相关文章:
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 类型字符串::长度()。我得到了"Unable to resolve identifier length"
- 如何掩盖对象数组的数据类型字符串到C 中的INT
- 解析参数值 - 字符串和链接值(类型字符串的链接值)与(特定类型的)变量
- C 中的枚举类型字符串类别
- 与基本面作斗争.特别是字符[],字符*和从数组中读取.也许我应该使用类型字符串
- 矢量类型 字符串,但当用整数初始化时
- 如何从const char *类型字符串中删除新线字符
- 在 c++ 中将 ostream 分配给数据类型字符串
- Linux 编译错误 - 无法将类型"字符串"转换为类型"字符串&&"
- LPCSTR 类型字符串出现乱码或显示错误
- C 表达式必须具有类型 - 字符串到const char*
- 将值分配给字符类型字符串
- 将组合框中的类型字符串与其项目进行比较
- 如何将返回类型字符串的数据复制到C++中
- 如何处理csv文件输入流,我需要将文件的每一行中的数据划分为6个不同的变量(类型字符串和int)
- 自定义C类型字符串类的问题
- 在c++中创建自定义C类型字符串类
- 在读取.csv时将数组类型字符串转换为double
- C++将类型字符串变量中的单词转换为小写