Watercleave
Watercleave

Reputation: 183

std::map<,>.emplace() producing error

I'm using a map to store data parsed from a text file. The actual code is pretty hefty, although I can post it if this isn't enough, but here's the outline of anything that contributes to the line that's causing the problem:

#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

}

This causes this compilation error:

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 &'

I suspect I'm using the wrong one of C++'s million variations on the theme of 'string', but I'm not sure and, if so, I have no idea what I am supposed to be using or how to fix it.

Upvotes: 1

Views: 643

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

This statement

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

is a function declaration that has return type map and one parameter of function type that also has the same return type. .

That it would be clear this construction

map<string,string>()

is a function declaration with an abstract declarator as a parameter of function .currentBlock.

Write simply

map<string,string> currentBlock;

The same is valid for lines

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

That is they are also function declarations having parameters with namea key and entry of type std::string

Upvotes: 5

Related Questions