没有重载函数getline c ++的实例

时间:2012-11-07 22:53:15

标签: c++

我对我的脚本错误导致此错误感到有点困惑。

我有一个函数可以调用填充游戏设置,但它不喜欢我的getline。

另外我应该提一下这些是我为它包含的文件:

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

这就是我所拥有的:

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);
}

我的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");

我哪里出错了?请帮忙!

错误:

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'

1 个答案:

答案 0 :(得分:1)

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

文件是为std::stringstd::ifstream定义的,请使用其他名称。

错误告诉您它无法将std::stringstd::getline函数的第一个参数匹配,该函数需要std::basic_istream<E,T>;

编辑:你也应该尝试与你更加一致std::前缀,字符串和ifstream都在命名空间中std::

相关问题