使用trim函数C ++删除空格时出错

时间:2012-11-08 02:26:05

标签: c++

我有一个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);
}

错误发生在第3个函数中,如:

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

任何想法我做错了什么?

2 个答案:

答案 0 :(得分:1)

var文件在哪里?

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

    std::map<string, string> config;

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

这里的文件是函数,getline需要basic_istream 因此失败

答案 1 :(得分: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的预期参数。