c++ - std::map<,>.emplace() 产生错误

标签 c++ string c++11

我正在使用 map 来存储从文本文件解析的数据。实际代码相当庞大,如果这还不够,我可以发布它,但这里是导致问题的代码行的任何内容的概述:

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <SDL.h>
#include <map>

using namespace std;

bool processTXT(char path[])
{
    ifstream source;
    source.open(path);

    char* key = new char[200];
    char* entry = new char[200];

    ifstream.getline(key, 200);
    ifstream.getline(entry, 200);

    //Not quite the original, but close enough; add lots of processing here

    map<string,string> currentBlock(map<string,string>());

    string keyS(string(key));
    string entryS(string(entry));
    currentBlock.emplace(keyS, entryS); //<----- THIS line seems to be the problem

    //Serialisation (of currentBlock) that I have yet to implement

}

这会导致此编译错误:

error C2664: 'std::pair<const _Kty,_Ty>::pair(const std::pair<const _Kty,_Ty> &)' :
cannot convert argument 1 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> '
to 'const std::string &'

我怀疑我在 C++ 的“字符串”主题的百万变体中使用了错误的一个,但我不确定,如果是这样,我不知道我应该使用什么或如何修复

最佳答案

这个声明

map<string,string> currentBlock(map<string,string>());

是一个函数声明,它具有返回类型映射和一个函数类型的参数,该函数类型也具有相同的返回类型。 .

这个结构很明显

map<string,string>()

是一个函数声明,带有抽象声明符作为函数.currentBlock 的参数。

简单写

map<string,string> currentBlock;

同样适用于行

string keyS(string(key));
string entryS(string(entry));

也就是说,它们也是函数声明,其参数带有 namea 键和 std::string 类型的条目

关于c++ - std::map<,>.emplace() 产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23769636/

相关文章:

c++ - 为什么 std::chrono::system_clock::to_time_t() 不是 constexpr?

c++ - 二维点的排列

c++ - 如何获取函数返回类型的模板成员的取消引用类型

c++ - 如何将一个非常大的二进制数转换为十进制数?

c++ - 用于字符串匹配算法的大 O 表示法

iOS:本地化多行字符串文字

c++ - 函数返回后未销毁 Char 数组

c++ - 使用模板模板参数的替代方法

c++ - WinDbg 和内联函数

c++ - POSIX:如何暂停线程?