与成员运营商*的模板化运营商*的朋友声明

时间:2014-01-19 19:21:15

标签: c++ templates operator-overloading

此问题附带的代码无法编译(g ++ 4.7),但有以下错误:

test.cpp:22:31: error: declaration of ‘operator*’ as non-function
test.cpp:22:31: error: expected ‘;’ at end of member declaration
test.cpp:22:32: error: expected unqualified-id before ‘<’ token
test.cpp: In function ‘int main(int, char**)’:
test.cpp:39:11: warning: unused variable ‘d’ [-Wunused-variable]
test.cpp: In instantiation of ‘Vector3<T> operator*(T, const Vector3<T>&) [with T = float]’:
test.cpp:37:16:   required from here
test.cpp:25:10: error: ‘float Vector3<float>::v [3]’ is private
test.cpp:30:57: error: within this context
test.cpp:25:10: error: ‘float Vector3<float>::v [3]’ is private
test.cpp:30:57: error: within this context
test.cpp:25:10: error: ‘float Vector3<float>::v [3]’ is private
test.cpp:30:57: error: within this context

但是,如果在operator*成员声明/定义之前移动了friend声明(只是交换了示例代码中的注释),那么它编译得很好。我不明白为什么。有没有办法使用位于operator*成员声明之后的朋友声明编译此代码?

template<typename T>
class Vector3;

template<typename T>
Vector3<T> operator*(T f, const Vector3<T>& v);

template<typename T>
class Vector3 {
public:
    Vector3(T x, T y, T z) {
        v[0] = x;
        v[1] = y;
        v[2] = z;
    }

  //friend Vector3<T> operator*<>(T f, const Vector3<T>& v); // WORKS

    T operator*(const Vector3<T>& other) const {
        return (v[0] * other.v[0] + v[1] * other.v[1] + v[2] * other.v[2]);
    }

    friend Vector3<T> operator*<>(T f, const Vector3<T>& v); // FAILS

private:
    T v[3];
};

template<typename T>
Vector3<T> operator*(T f, const Vector3<T>& v) {
    return Vector3<T>(f * v.v[0], f * v.v[1], f * v.v[2]);
}

int main(int argc, char *argv[]) {
    Vector3<float> v(0, 0, 0);
    Vector3<float> w(0, 0, 0);

    w = 2.0f * v;

    float d = v * w;

    return 0;
}

1 个答案:

答案 0 :(得分:2)

问题是你没有指定你的意思是来自全局命名空间的operator *,要把你的朋友声明改为:

friend Vector3<T> (::operator*<>)(T f, const Vector3<T>& v);

当你在operator *成员之前声明它时,没有歧义,所以它被正确选中了。