我的编译器是否与它认为重载函数混淆了?

时间:2012-01-09 01:28:08

标签: c++ function compiler-errors overloading

我有以下标题功能:

float computeDistance3(Vector3f& vec_a, Vector3f& vec_b);

float computeDotProduct3(Vector3f& vecta, Vector3f& vectb);

float computeGeoDotProd3(Vector3f& vecta, Vector3f& vectb);

具有以下定义

float computeDistance3(Vector3f& vec_a, Vector3f& vec_b) {
    float x = vec_a.x - vec_b.x;
    float y = vec_a.y - vec_b.y;
    float z = vec_a.z - vec_b.z;

    return sqrt((x * x) + (y * y) + (z * z));
}

float computeDotProduct3(Vector3f& vec_a, Vector3f vec_b) {
    return (vec_a.x * vec_b.x) 
         + (vec_a.y * vec_b.y) 
         + (vec_a.z * vec_b.z);
}

float computeGeoDotProd3(Vector3f& vecta, Vector3f& vectb) {
    float amag, bmag, dotProd;

    amag = vecta.computeMagnitude();
    bmag = vectb.computeMagnitude();

    dotProd = computeDotProduct3(vecta, vectb);

    bool notZero = (amag != 0.0f && bmag != 0.0f) && dotProd != 0.0f;

    if (notZero) {
        return cosf(dotProd / (amag * bmag));
    } else {
        return -1.0f;   
    }

}

我知道他们的签名是一样的。这会让编译器感到困惑吗?我猜是这样的,因为当我编译代码时,我得到了这个:

vector3f.cpp: In function ‘float computeGeoDotProd(Vector3f&, Vector3f&)’:                                                                                                  
vector3f.cpp:139:43: error: call of overloaded ‘computeDotProduct3(Vector3f&, Vector3f&)’ is ambiguous                                                                      
vector3f.cpp:139:43: note: candidates are:                                                                                                                                  
vector3f.h:31:7: note: float computeDotProduct3(Vector3f&, Vector3f&)                                                                                                       
vector3f.cpp:127:7: note: float computeDotProduct3(Vector3f&, Vector3f)   

问题

解除编译器混淆的解决方案是什么?

1 个答案:

答案 0 :(得分:5)

您在定义中遗漏了&

float computeDotProduct3(Vector3f& vec_a, Vector3f vec_b) {

应该是:

float computeDotProduct3(Vector3f& vec_a, Vector3f& vec_b) {

所以你最终会得到两个不同的(重载的)函数原型,它们只有引用&不同 - 因此含糊不清。