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

次のように、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);

私が間違っていることはありますか?

4

2 に答える 2

1

私はあなたのsettings.hファイルに基づいて 2 つと 2 つをつなぎ合わせています...関数ヘッダーを書き出すのを忘れていると思います。このビット:

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 に答える
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 に答える