命名空间内的
项目中有一个round
函数在一个单独的命名空间中:
namespace CMMN
{
inline long round (double x) { return long (x < 0. ? x-0.5 : x+0.5); }
}
某些类在*.h文件中声明,其成员函数在单独的*.cpp文件中定义:
using CMMN::round;
long SOME_CLASS::MemberFunction()
{
return round(sqrt(m_SomeValue));
}
问题是编译器生成了一个错误:
error C2668: 'CMMN::round' : ambiguous call to overloaded function
...commdef.h(222): could be 'long CMMN::round(double)'
C:Program Files (x86)Microsoft Visual Studio 12.0VCincludemath.h(1241): or 'long double round(long double) throw()'
C:Program Files (x86)Microsoft Visual Studio 12.0VCincludemath.h(1125): or 'float round(float) throw()'
C:Program Files (x86)Microsoft Visual Studio 12.0VCincludemath.h(516): or 'double round(double)'
while trying to match the argument list '(double)'
- 我想知道为什么在cpp文件的开头使用ussing声明不影响下面的memeber函数定义
- 有没有办法是否在成员函数定义中看到使用声明为里面的每个函数定义添加using声明是一个决定,但它几乎等同于完全限定的
round
调用
提前谢谢。
它确实会影响它。编译器告诉你有4个候选函数可以调用,但它不知道该调用哪一个。其中一个候选者是CMMN::round,它表明正在识别using声明。
你必须消除你想让它调用哪个round()
的歧义。
using CMMN::round
仅使该符号在所述命名空间内可见;它不影响绑定偏好。
您似乎还在某个地方包含了<cmath>
或<math.h>
,它们也声明了round
的重载。
您必须在函数定义中放置using声明,或者在使用它们的地方完全限定它们的名称。