C 模板问题

c++ template issue

本文关键字:问题      更新时间:2023-10-16

我当前正在使用本教程中给出的类:http://www.dreamincode.net/forums/topic/183191-create-create-create-a-a-simple-confile-file-parser-file-parser-file-parser/

最初工作正常,但是由于我将单源文件分开为单独的标头和CPP文件,因此我一直无法调用getValueOfkey函数

标题:

#ifndef CONFIGFILE_H
#define CONFIGFILE_H
#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <fstream>
#include <typeinfo>
class ConfigFile
{
private:
        std::map<std::string, std::string> contents;
    std::string fName;
    void removeComment(std::string &line) const;
    bool onlyWhitespace(const std::string &line) const;
    bool validLine(const std::string &line) const;
    void extractKey(std::string &key, size_t const &sepPos, const std::string &line) const;
    void extractValue(std::string &value, size_t const &sepPos, const std::string &line) const;
    void extractContents(const std::string &line);
    void parseLine(const std::string &line, size_t const lineNo);
    void ExtractKeys(); 
public:
    ConfigFile(const std::string &fName);
    bool keyExists(const std::string &key) const;
    template <typename ValueType>
    ValueType getValueOfKey(const std::string &key, ValueType const &defaultValue) const;
};

#endif  /* CONFIGFILE_H */

cpp:

#include "ConfigFile.h"
std::map<std::string, std::string> contents;
std::string fName;
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))
        std::cout << "CFG: Not a valid " << (std::string)typeid (T).name() << " received!n" << std::endl;
    return returnVal;
}
template <>
std::string string_to_T(std::string const &val)
{
    return val;
}
void ConfigFile::removeComment(std::string &line) const
{
    if (line.find(';') != line.npos)
        line.erase(line.find(';'));
}
bool ConfigFile::onlyWhitespace(const std::string &line) const
{
    return (line.find_first_not_of(' ') == line.npos);
}
bool ConfigFile::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 ConfigFile::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 ConfigFile::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 ConfigFile::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
        std::cout << "CFG: Can only have unique key names!n" << std::endl;
}
void ConfigFile::parseLine(const std::string &line, size_t const lineNo)
{
    if (line.find('=') == line.npos)
        std::cout << "CFG: Couldn't find separator on line: " << T_to_string(lineNo) << "n" << std::endl;
    if (!validLine(line))
        std::cout << "CFG: Bad format for line: " << T_to_string(lineNo) << "n" << std::endl;
    extractContents(line);
}
void ConfigFile::ExtractKeys()
{
    std::ifstream file;
    file.open(fName.c_str());
    if (!file)
        std::cout << "CFG: File " << fName << " couldn't be found!n" << std::endl;
    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();
}
ConfigFile::ConfigFile(const std::string &fName)
{
    this->fName = fName;
    ExtractKeys();
}
bool ConfigFile::keyExists(const std::string &key) const
{
    return contents.find(key) != contents.end();
}
template <typename ValueType>
ValueType ConfigFile::getValueOfKey(const std::string &key, ValueType const &defaultValue = ValueType()) const
{
    if (!keyExists(key))
        return defaultValue;
    return string_to_T<ValueType> (contents.find(key)->second);
}

我试图使用与单个文件时相同的方法来调用它,例如std::cout << Config.getValueOfKey<std::string>("test");,但是现在我收到以下编译器错误

main.cpp: In function 'int main(int, char**)':
main.cpp:29:71: error: no matching function for call to 'ConfigFile::getValueOfKey(const char [5])'
main.cpp:29:71: note: candidate is:
In file included from main.h:17:0,
                 from main.cpp:9:
ConfigFile.h:35:12: note: template<class ValueType> ValueType ConfigFile::getValueOfKey(const string&, const ValueType&) const
ConfigFile.h:35:12: note:   template argument deduction/substitution failed:
main.cpp:29:71: note:   candidate expects 2 arguments, 1 provided

鉴于我对模板上的不良掌握,我真的看不到这个错误试图告诉我什么,我尝试传递直接字符串而不是char阵列。任何帮助或解释都将不胜感激,在过去的几个小时中,我的头在桌子上戴了一个漂亮的孔。

您声明了2个参数的方法:

ValueType getValueOfKey(const std::string &key, ValueType const &defaultValue) const;
//                                          |                           |
//                                   first parameter             second parameter

只提供一个:

Config.getValueOfKey<std::string>("test");

我还没有遇到一个编译器,该编译器猜测您没有任何帮助的意思。

您需要将默认值移至标头文件,在此声明该方法:

ValueType getValueOfKey(const std::string &key, ValueType const &defaultValue = ValueType()) const;
    //                                          |                           |
    //                                   first parameter             second parameter

之后您可能会收到一个链接器错误,因此您可能需要检查一下。