我正在开发一个适用于多种算术类型的项目。所以我做了一个标题,其中定义了用户定义的算术类型的最低要求:
user_defined_arithmetic.h:
typedef double ArithmeticF; // The user chooses what type he
// wants to use to represent a real number
namespace arithmetic // and defines the functions related to that type
{
const ArithmeticF sin(const ArithmeticF& x);
const ArithmeticF cos(const ArithmeticF& x);
const ArithmeticF tan(const ArithmeticF& x);
...
}
困扰我的是,当我使用这样的代码时:
#include "user_defined_arithmetic.h"
void some_function()
{
using namespace arithmetic;
ArithmeticF lala(3);
sin(lala);
}
我得到一个编译器错误:
error: call of overloaded 'sin(ArithmeticF&)' is ambiguous
candidates are:
double sin(double)
const ArithmeticF arithmetic::sin(const ArithmeticF&)
我从来没有使用过<math.h>
标题,只有<cmath>
. 我从来没有using namespace std
在头文件中使用过。
我正在使用 gcc 4.6.*。我检查了包含歧义声明的标题是什么,结果是:
数学调用.h:
Prototype declarations for math functions; helper file for <math.h>.
...
我知道,这<cmath>
包括<math.h>
,但它应该屏蔽 std 命名空间的声明。我深入研究<cmath>
标题并发现:
cmath.h:
...
#include <math.h>
...
// Get rid of those macros defined in <math.h> in lieu of real functions.
#undef abs
#undef div
#undef acos
...
namespace std _GLIBCXX_VISIBILITY(default)
{
...
所以命名空间std在. #include <math.h>
这里有什么问题,还是我误解了什么?