首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::map<,>.emplace()产生错误

std::map<,>.emplace()产生错误
EN

Stack Overflow用户
提问于 2014-05-21 04:46:23
回答 1查看 465关注 0票数 1

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

代码语言:javascript
复制
#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

}

这会导致此编译错误:

代码语言:javascript
复制
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++的数百万个变体中的错误一个,但是我不确定,如果是这样的话,我不知道我应该使用什么或者如何修复它。

EN

回答 1

Stack Overflow用户

发布于 2014-05-21 04:51:49

这句话

代码语言:javascript
复制
map<string,string> currentBlock(map<string,string>());

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

很明显,这个结构

代码语言:javascript
复制
map<string,string>()

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

写得简单

代码语言:javascript
复制
map<string,string> currentBlock;

同样的道理也适用于行

代码语言:javascript
复制
string keyS(string(key));
string entryS(string(entry));

也就是说,它们也是具有名称、键和std::string类型条目的参数的函数声明

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23769636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档