C++ 没有重载函数getline c++;
我有点困惑,我的脚本有什么不正确的地方导致了这个错误 我有一个函数,调用游戏设置填充,但它不喜欢我的getline 此外,我还应提及我为其提供的文件:C++ 没有重载函数getline c++;,c++,C++,我有点困惑,我的脚本有什么不正确的地方导致了这个错误 我有一个函数,调用游戏设置填充,但它不喜欢我的getline 此外,我还应提及我为其提供的文件: #include <fstream> #include <cctype> #include <map> #include <iostream> #include <string> #include <algorithm> #include <vector> usi
#include <fstream>
#include <cctype>
#include <map>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;'
我哪里出错了?请帮忙
错误:
settings.h(61):错误C2784:'std::basic_istream&std::getline(std::basic_istream&&,std::basic_string&'):无法从'std::string'推断'std::basic_istream&'的模板参数
标准::地图加载设置(标准::字符串文件){
std::ifstream文件(file);
文件是为std::string
和std::ifstream
定义的,使用不同的名称
错误告诉您它无法将std::string
与std::getline
函数的第一个参数匹配,该函数需要std::basic_istream
编辑:您也应该尝试与您保持一致
std::
前缀、string和ifstream都在名称空间std::
我们在这里讨论的是std::string,对吗?@BjoernD抱歉,我以为我添加了错误,请参见编辑:)除非他正在编译C++11,否则他需要传递file.C_str()
@jrok我假设是这样,否则我们也会听到这个错误。所以这个加载设置(std::string文件)
需要加载设置(std::ifstream文件)
?@Dave不,做一些类似加载设置(std::string文件路径){std::ifstream文件(文件路径);
std::map加载设置(std::ifstream dir)
和ifstream文件(dir);
像这样吗?
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);
}
//load settings for game
std::map<string, string> config = loadSettings("settings.txt");
//load theme for game
std::map<string, string> theme = loadSettings("theme.txt");
std::map<string, string> loadSettings(std::string file){
std::ifstream file(file);