A S*_*ity 0 c++ templates operators visual-studio-2008 visual-c++
我在Visual Studio 2008/Windows SDK 7上编译以下代码时出现以下错误
const UINT a_uint;
UINT result;
throw std::runtime_error( std::string("we did ") + a_uint +
" and got " + result );
Run Code Online (Sandbox Code Playgroud)
具有讽刺意味的是,我最终得到了这个结果
error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(
const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem
)' : template parameter '_Elem' is ambiguous
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么错误消息不能解释真正的问题(没有运算符来输入字符串)?
你可以减少它
template<typename T>
void f(T, T);
int main() {
f('0', 0); // is T int or char?
}
Run Code Online (Sandbox Code Playgroud)
您尝试将unsigned int添加到字符串中.这没有意义,并且std::string该类不需要采取任何预防措施来向char此处添加隐式转换,因为这会隐藏这些潜在的编程错误.
尝试将unsigned int std::string转换为十进制/十六进制/八进制/ etc形式,然后连接(您可以使用std::ostringstream或执行此操作boost::lexical_cast)或以您认为合适的其他方式修复错误.