C++ 解析配置文件时出错

C++ 解析配置文件时出错,c++,fileparsing,C++,Fileparsing,我的一些样本编码有一些问题。理想情况下,我想要一个文件,将有关键字,有数据整型和字符串与他们可以改变。我将有duration int、destinationstring和SourceSourceSource,可以随时编辑。这最终是为了便于使用,以天为单位在设定的时间内备份文件,备份文件的位置和去向 这是我的示例代码,我已经修复了一些错误,但在最后两个错误上仍有困难。还是有更简单的方法?对不起,所有的杂乱,我是新的C++……/P> 谢谢你的帮助,我知道有很多事情要经历。也许有人有一个关于简单配置文

我的一些样本编码有一些问题。理想情况下,我想要一个文件,将有关键字,有数据整型和字符串与他们可以改变。我将有duration int、destinationstring和SourceSourceSource,可以随时编辑。这最终是为了便于使用,以天为单位在设定的时间内备份文件,备份文件的位置和去向

这是我的示例代码,我已经修复了一些错误,但在最后两个错误上仍有困难。还是有更简单的方法?对不起,所有的杂乱,我是新的C++……/P> 谢谢你的帮助,我知道有很多事情要经历。也许有人有一个关于简单配置文件解析的好教程,我也可以读一下

我的gcc版本是4.4.7

我收到的错误有:

g++configFile.cpp configFile.cpp:36:错误:非命名空间作用域“class Convert”中的显式专门化 configFile.cpp:在静态成员函数“static T Convert::string_to_Tconst std::string&”中: configFile.cpp:31:错误:“exitWithError”没有依赖于模板参数的参数,因此“exitWithError”的声明必须可用 configFile.cpp:31:注意:如果使用“-fppermissive”,G++将接受您的代码,但不赞成使用未声明的名称

#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class Convert
{

public:
template <typename T>
static std::string T_to_string(T const &val) 
{
    std::ostringstream ostr;
    ostr << val;

    return ostr.str();
}

template <typename T>
static T string_to_T(std::string const &val) 
{
    std::istringstream istr(val);
    T returnVal;

    if (!(istr >> returnVal))
        exitWithError("CFG: Not a valid " + (std::string)typeid(T).name() + " received!\n");

    return returnVal;
}

template <>
static std::string string_to_T(std::string const &val)
{

    return val;
}
};

void exitWithError(const std::string &error) 
{
std::cout << error;
std::cin.ignore();
std::cin.get();

exit(EXIT_FAILURE);
}

class ConfigFile
{

private:
std::map<std::string, std::string> contents;
std::string fName;

void removeComment(std::string &line) const
{
    if (line.find(';') != line.npos)
        line.erase(line.find(';'));
}

bool onlyWhitespace(const std::string &line) const
{
    return (line.find_first_not_of(' ') == line.npos);
}

bool validLine(const std::string &line) const
{
    std::string temp = line;
    temp.erase(0, temp.find_first_not_of("\t "));

    if (temp[0] == '=')

        return false;

    for (size_t i = temp.find('=') + 1; i < temp.length(); i++)

                    if (temp[i] != ' ')

            return true;

    return false;
}

void extractKey(std::string &key, size_t const &sepPos, const std::string &line) const
{
    key = line.substr(0, sepPos);
    if (key.find('\t') != line.npos || key.find(' ') != line.npos)
        key.erase(key.find_first_of("\t "));
}
void extractValue(std::string &value, size_t const &sepPos, const std::string &line) const
{
    value = line.substr(sepPos + 1);
    value.erase(0, value.find_first_not_of("\t "));
    value.erase(value.find_last_not_of("\t ") + 1);
}
void extractContents(const std::string &line) 
{
    std::string temp = line;
    temp.erase(0, temp.find_first_not_of("\t "));
    size_t sepPos = temp.find('=');
    std::string key, value;
    extractKey(key, sepPos, temp);
    extractValue(value, sepPos, temp);

    if (!keyExists(key))
        contents.insert(std::pair<std::string, std::string>(key, value));
    else
        exitWithError("CFG: Can only have unique key names!\n");
}
void parseLine(const std::string &line, size_t const lineNo)
{
    if (line.find('=') == line.npos)
        exitWithError("CFG: Couldn't find separator on line: " + Convert::T_to_string(lineNo) + "\n");

    if (!validLine(line))
        exitWithError("CFG: Bad format for line: " + Convert::T_to_string(lineNo) + "\n");

    extractContents(line);
}
void ExtractKeys()
{
    std::ifstream file;
    file.open(fName.c_str());
    if (!file)
        exitWithError("CFG: File " + fName + " couldn't be found!\n");

    std::string line;
    size_t lineNo = 0;
    while (std::getline(file, line))
    {

        lineNo++;
        std::string temp = line;
        if (temp.empty())
            continue;

        removeComment(temp);
        if (onlyWhitespace(temp))
            continue;

        parseLine(temp, lineNo);
    }
    file.close();
}
public:
ConfigFile(const std::string &fName)
{
    this->fName = fName;
    ExtractKeys();
}
bool keyExists(const std::string &key) const
{
    return contents.find(key) != contents.end();
}
template <typename ValueType>

ValueType getValueOfKey(const std::string &key, ValueType const &defaultValue = ValueType()) const
{
    if (!keyExists(key))
        return defaultValue;

    return Convert::string_to_T<ValueType>(contents.find(key)->second);
}
};

int main()
{

ConfigFile cfg("config.cfg");



bool exists = cfg.keyExists("car");

std::cout << "car key: " << std::boolalpha << exists << "\n";

exists = cfg.keyExists("fruits");

std::cout << "fruits key: " << exists << "\n";



std::string someValue = cfg.getValueOfKey<std::string>("mykey", "Unknown");

std::cout << "value of key mykey: " << someValue << "\n";

std::string carValue = cfg.getValueOfKey<std::string>("car");

std::cout << "value of key car: " << carValue << "\n";

double doubleVal = cfg.getValueOfKey<double>("double");

std::cout << "value of key double: " << doubleVal << "\n\n";



std::cin.get();

return 0;

}

使用以下代码,您是否尝试对其进行专门化

template <>
static std::string string_to_T(std::string const &val)
{

    return val;
}
此外,在Convert或forward声明中使用exitWithError之前,还需要exitWithError,因为在Convert中使用它时编译器尚未看到它。将其移动到“转换”的定义之前:


感谢您用exitWithError捕捉到我的放置错误。对于专门化错误,我不必专门化它。在我的研究中,这似乎是实现我目标的好方法。你有其他的建议吗?对于显式专门化,我仍然会遇到任何错误,但当我能够找出错误时,我会报告。谢谢你,杰西。@user2630969:专业化看起来是一条路要走。谢谢你,杰西,这正是我想要做的。给你带来一些好的感觉!
template <>
std::string Convert::string_to_T<std::string>(std::string const &val)
{

    return val;
}
void exitWithError(const std::string &error) 
{
std::cout << error;
std::cin.ignore();
std::cin.get();

exit(EXIT_FAILURE);
}
// Or just forward declare void exitWithError(const std::string &);

class Convert
{ ....