c++ - 使用 C++ 函数模板时的转换错误

标签 c++ templates casting xcode5

这是我在这里的第一篇文章。实际上感觉有点尴尬;-)

我正在为 C++ 模板而苦苦挣扎。我有一个 'textSettings" 类,它读取值对的 TXT 文件,每个都在它的行上,并将它们放在 std::map 中,该映射使用 std::string 作为键和值。

文本文件中可能的入口可以是例如 GRID_SIZE 6,因此 map 的键是字符串 "GRID_SIZE",值是字符串“6”

在我的程序中,我想将它们用作实际值的类型:int/float/bool/string

程序只希望将这 4 种类型作为输入。因此,我使用模板化函数通过它的键在映射中找到某个值,并且只为这些类型实例化它。

在这个函数中,我使用 std::tr1::is_same 检查类型,以便能够正确解析值。

它在 XCode 提示的 else 子句处失败,抛出三个错误。它不能将 val 转换为 float、int 或 bool。错误看起来都一样:

/src/textSettings.cpp:82:17: Assigning to 'int' from incompatible type 'std::string' (aka 'basic_string') /src/textSettings.cpp:82:17: Assigning to 'float' from incompatible type 'std::string' (aka 'basic_string') /src/textSettings.cpp:82:17: Assigning to 'bool' from incompatible type 'std::string' (aka 'basic_string')

然而,所有提示的类型都已由其他 if 子句处理。我真的不知道该如何处理。我仍在学习 C++ 和模板,所以我可能会忽略一些明显的东西。 如果我注释掉这一行,程序就会完美地编译和链接。显然我无法解析文本文件中的值...

我正在使用 XCode 5.1.1

textSettings.h

template<typename T>
bool findValue(const std::string& key, T& val);

textSetting.cpp

template<typename T>
bool textSettings::findValue(const std::string &key, T& val) {
    std::map<std::string, std::string>::iterator it;
    it = data.find(key);
    if (it != data.end()) {
        if (std::tr1::is_same<T, int>::value) {
            val = atoi(it->second.c_str());
        }
        else if (std::tr1::is_same<T, float>::value) {
            val = atof(it->second.c_str());
        }
        else if (std::tr1::is_same<T, bool>::value) {
            val = static_cast<bool>(atoi(it->second.c_str()));
        }
        else if (std::tr1::is_same<T, std::string>::value) {
            val = it->second; // <- ERROR HERE
        }
        else {
            printf("Textsettings:: Error, type unknown!\n");
            return false;
        }
        return true;
    }
    return false;
}
template bool textSettings::findValue<int>(const std::string&, int&);
template bool textSettings::findValue<float>(const std::string&, float&);
template bool textSettings::findValue<bool>(const std::string&, bool&);
template bool textSettings::findValue<std::string>(const std::string&, std::string&);

感谢您的意见

最佳答案

您收到此错误,因为对于 std::string 以外的实例化, 你正在分配 it->second ,即 std::string val 引用的变量这是不可分配给字符串类型( floatintbool )。

您的情况std::tr1::is_same<T, std::string>::value将评估为 false并不意味着您没有义务在条件 block 内提供词法正确的代码。

通过为每个需要的类型创建单独的函数重载并使用重载函数调用来自动匹配所需的类型,可以更好地解决此类问题:

bool ParseStr(const std::string& str, int& val) {
   val = atoi(str.c_str());
   return true;
}
bool ParseStr(const std::string& str, float& val) {
   val = atof(str.c_str());
   return true;
}
bool ParseStr(const std::string& str, bool& val) {
   val = static_cast<bool>(atoi(str.c_str()));
   return true;
}
bool ParseStr(const std::string& str, std::string& val) {
   val = str;
   return true;
}
template <typename T>
bool ParseStr(const std::string& str, T& val) {
   printf("Textsettings:: Error, type unknown!\n");
   return false;
}

template<typename T>
bool textSettings::findValue(const std::string &key, T& val) {
  std::map<std::string, std::string>::iterator it;
  it = data.find(key);
  if (it != data.end()) {
     return ParseStr(it->second, val);
  }
  return false;
 }

关于c++ - 使用 C++ 函数模板时的转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25855633/

相关文章:

c++ - opencv 命名空间和 c、c++ 函数

php - 使用 SWIG 创建 PHP C/C++ 扩展模块

c++ - 为什么 C++ 支持带实现的纯虚函数?

c++ - 可变默认参数

c++ - 归并排序。使用迭代器实现

c# 在运行时创建一个未知的泛型类型

c++ - 获得更好的 iostream 错误消息

ruby-on-rails - Rails 条件布局 : Why does ":for" work as an option for the layout method?

c++将基类转换为派生类困惑

c++ - 是原始类型转换,在内存中创建一个新对象吗?