错误:"2 overloads have similar conversions"
Error: "2 overloads have similar conversions"
编辑:
当我写问题时,我注意到方法std::string GetNodeValue(const std::string& nodePath, const char * defaultValue)
不是常量。正如LogicStuff在他的评论中也提到的那样,添加const
资格解决了歧义。
我知道这个问题已经在这里和其他几次被问过并正确回答了。我理解潜在的问题,但我不太清楚为什么它会在这种特殊情况下发生,它唤醒了我好奇的自我。
我有以下课程:
class ConfigurationReader
{
public:
// ...
std::string GetNodeValue(const std::string& nodePath, const char * defaultValue)
{
const std::string temp(defaultValue);
return GetNodeValue(nodePath, temp);
}
template <typename T> T GetNodeValue(const std::string & nodePath, T defaultValue) const
{
boost::optional<T> nodeValue = configuration.getNodeValueNothrow<T>(nodePath);
if ( nodeValue )
{
return *nodeValue;
}
LogConfigurationProblemsCri(logger, "Node not found: " << nodePath << ", Default value: " << defaultValue);
return defaultValue;
}
// ...
};
模板方法还对类型int16_t
、uint16_t
等进行了uint64_t
使用时它就像一个魅力:
string someValue = configurationReaderPtr->GetNodeValue("some_noe", "");
uint32_t otherValue = configurationReaderPtr->GetNodeValue("other_node", 11000);
bool yetAnother = configurationReaderPtr->GetNodeValue("other_node", true);
除一种情况外:
uint32_t otherValue = configurationReaderPtr->GetNodeValue("other_node", 0);
我不断得到的错误是:"2 个重载具有类似的转换可能是'std::string ConfigurationReader::GetNodeValue(const std::string &,const char *(' or 'uint32_t ConfigurationReader::GetNodeValue(const std::string &,uint32_t( const'">
我尝试转换"默认"值:uint32_t(0)
、static_cast<uint32_t>(0)
、0U
,但没有任何运气。
我应该指出,我已经找到了解决方法:
uint32_t otherValue = 0;
otherValue = configurationReaderPtr->GetNodeValue("other_node", otherValue);
但这并不能回答我的好奇心。我目前正在使用Microsoft Visual Studio 2012 Express和boost 1.54库。
有什么想法吗?
0 是空指针的文字(在现代C++中被替换为"nullptr"(。
所以 0 可以是 int 或空指针,尤其是 char*
编辑以添加一些参考:您可以在标准中找到它作为
4.10 指针转换 空指针常量是值为零的整数文本 (2.13.2( 或 std::nullptr_t 类型的 prvalue
(最后一个是对nullptr
的引用(
两种重载被认为同样可行
configurationReaderPtr->GetNodeValue("other_node", 0);
因为:
需要从
0
的隐式转换,其类型为int
到const char *
。需要从
ConfigurationReader*
到ConfigurationReader const*
的隐式转换(以调用const
限定成员函数(
在使两个重载(平等(const
限定后,代码将编译(首选函数模板(。第一个重载也不会首先修改任何成员。
住在科里鲁
在您的特定示例中,将字符串版本的签名更改为
std::string GetNodeValue(const std::string& nodePath, const std::string defaultValue)
这将消除任何歧义。
- 需要帮助了解"{}-initializer syntax minimizes the chances for unfortunate conversions"
- Box2d c++ what is the similar to ActionScript b2Body SetPosi
- 错误:"2 overloads have similar conversions"
- 最新草案§8.5.3/5第(5.2.3/5)段(5.2.2.1)中的"user-defined conversions are not considered"是什么意思?
- 是否可以防止此代码"copy and paste similar template special case"?
- Unicode conversions - iconv_xxx versus mbstowcs/wcstombs