简体   繁体   中英

no instance of overloaded function getline c++

I'm a bit confused as to what i have incorrect with my script that is causing this error.

I have a function which calls a fill for game settings but it doesn't like my getline.

Also i should mention these are the files i have included for it:

#include <fstream>
#include <cctype>
#include <map>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;'

This is what i have:

std::map<string, string> loadSettings(std::string file){

ifstream file(file);
string line;

std::map<string, string> config;

while(std::getline(file, line))
{
    int pos = line.find('=');
    if(pos != string::npos)
    {
        string key = line.substr(0, pos);
        string value = line.substr(pos + 1);
        config[trim(key)] = trim(value);
    }
}
return (config);
}

The function is called like this from my main.cpp

 //load settings for game
 std::map<string, string> config = loadSettings("settings.txt");
 //load theme for game
 std::map<string, string> theme = loadSettings("theme.txt");

Where did i go wrong ? Please help!

The error:

settings.h(61): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'std::string'

std::map<string, string> loadSettings(std::string file){
    std::ifstream file(file);

File is defined for a std::string and a std::ifstream , give on a different name.

The error is telling you it can not match the std::string to the first param of the std::getline function which expects a std::basic_istream<E,T> ;

EDIT: you should be also try and be more consistant with you std:: prefixing, string and ifstream are both in the namespace std::

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM