-1

我有一个 cpp 文件,它可以抓取线条并修剪它们,但我遇到了错误。

错误说:

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 'overloaded function type' from 'overloaded function type'

我不确定这意味着什么......但这是脚本......:

#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <cctype>
#include "settings.h"

using namespace std;

// trim from start
static inline std::string& line_trim(std::string& s) {

    //erase 
    s.erase(
            //pointer start location
            s.begin(),

            //find from pointer which is set to begin
                std::find_if( s.begin(), 
            //check to the end
                s.end(), 
            //look for spaces
                std::not1( std::ptr_fun<int, int>(std::isspace) ) ) );

    //return the result
        return s;
}

// trim from end
static inline std::string& end_trim(std::string& s) {

    //erase   
    s.erase(

            //find from the pointer set to end of line
                std::find_if(s.rbegin(),

            //check to the beginning (because were starting from end of line
                s.rend(), 

            //look for spaces
                std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
                s.end());
        return s;
}


static inline std::string& trim(std::string &s) {
    return line_trim(end_trim(s));
}

ifstream file(std::string file_path){
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);
}

该错误发生在第三个函数中,例如:

while(std::getline(file, line))

编辑:这是我的settings.h

#include <map>
using namespace std;

static inline std::string &line_trim(std::string&);

static inline std::string &end_trim(std::string&);

static inline std::string &trim(std::string&);

std::map<string, string> loadSettings(std::string);

任何想法我做错了什么?

4

2 回答 2

1

var文件在哪里?

ifstream file(std::string file_path){
string line;

    std::map<string, string> config;

    while(std::getline(file, line))

在这里文件是函数,getline 期望 basic_istream 因此失败

于 2012-11-08T02:33:29.840 回答
1

我根据您的settings.h文件将两个和两个拼凑在一起......我认为您忘记写出您的函数标题。这一点:

ifstream file(std::string file_path){
string line;

    std::map<string, string> config;

应该:

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

对于您当前的实现,file是一个函数(因为它具有从开头开始的范围{),所以它不是getline.

于 2012-11-08T02:37:07.173 回答