首页 > 解决方案 > 在模板类中使用友元运算符重载函数,错误未定义引用

问题描述

这是我的代码和声明,我不确定如何使用模板类正确设置友元函数。有人可以指导我做错了什么,为什么?

谢谢!


private:
    int size;   
    T* buff; 
    friend Vector<T> operator * (const int n, const Vector<T> & v);

    friend Vector<T> operator + (const int n, const Vector<T> & v);


template<typename T>
Vector<T> operator * (const int n, const Vector<T> & v){
    Vector<T> vec(v.size);
    vec.size = v.size;
    for(int i = 0;i<vec.size;i++){
        vec.buff[i] = v.buff[i]*n;
    }
}
template<typename T> 
Vector<T> operator+ (const int n, const Vector<T> & v){
    Vector<T> vec(v.size);
    vec.size = v.size;
    for(int i = 0;i<vec.size;i++){
        vec.buff[i] = v.buff[i]*n;
    }
}

标签: c++templatesoverloadingfriend

解决方案


推荐阅读