模板上的算术运算符过载,导致无法解析的外部错误

Overloaded arithmetic operators on a template causing an unresolved external error

本文关键字:错误 外部 运算符      更新时间:2023-10-16

假设我有这个类:

Vector4.h中:

#pragma once
template<typename T>
class Vector4
{
public:
T X;
T Y;
T Z;
T W;
Vector4();
Vector4(T X, T Y, T Z, T W);
~Vector4();
friend Vector4<T> operator*(const Vector4<T>& l, const Vector4<T>& r);
};
#include "Vector4.inl"

并且在Vector4.inl中:

template<typename T>
Vector4<T>::Vector4()
{
X = 0;
Y = 0;
Z = 0;
W = 0;
}
template<typename T>
Vector4<T>::Vector4(T X, T Y, T Z, T W)
{
   this->X = X;
   this->Y = Y;
   this->Z = Z;
   this->W = W;
}
template<typename T>
Vector4<T>::~Vector4()
{
}
template<typename T>
Vector4<T> operator*(const Vector4<T>& l, const Vector4<T>& r)
{
     return(Vector4<T>(l.X * r.X, l.Y * r.Y, l.Z * r.Z, l.W * r.W));
}

当我在这样的地方使用它时:

Vector4<float> a, b;
a = a * b;

它给了我一个LNK2019未解析的外部符号我做错了什么?我使用的语法不正确吗?

如注释所述,您的朋友函数声明

friend Vector4<T> operator*(const Vector4<T>& l, const Vector4<T>& r);

在全局命名空间中声明了一个非模板函数。当您实例化例如Vector4<int>时,函数

Vector4<int> operator*(const Vector4<int>& l, const Vector4<int>& r)

已声明。请注意,它不是函数模板。(另请参阅[临时朋友])

然后Vector4.inl声明并定义一个函数模板

template<typename T>
Vector4<T> operator*(const Vector4<T>& l, const Vector4<T>& r)

即前一功能的过载。在表达式a * b中,重载解析选择非模板operator*而不是模板版本(请参见[over.match.best]/1)。这会导致链接器错误,因为尚未定义非模板函数。


正如我几乎愚弄了自己一样,一句简短的话:

template<typename T>
Vector4<T> operator*(const Vector4<T>& l, const Vector4<T>& r);

由于这个操作符是一个自由函数(非成员函数),这两行声明了一个函数模板,很像

template<typename T>
Vector4<T> wup();

另一方面,

template<typename T>
Vector4<T> Vector4<T>::operator*(const Vector4<T>& r)
{ /* ... */ }

定义类模板(Vector4)的成员函数(非模板)。


一种解决方案是使用前向声明,只与特定的专业化交朋友:

template<typename T>
class Vector4;
template<typename T>
Vector4<T> operator*(const Vector4<T>& l, const Vector4<T>& r);

template<typename T>
class Vector4
{
public:
    T X;
    T Y;
    T Z;
    T W;
    Vector4();
    Vector4(T X, T Y, T Z, T W);
    ~Vector4();
    // compiler knows of some function template `operator*`,
    // can name an specialization:
    // ~~~~~~~~~~~~~~~~~~~~~~~~vvv
    friend Vector4<T> operator*<T>(const Vector4<T>& l, const Vector4<T>& r);
};
template<typename T>
Vector4<T>::Vector4()
{
    X = 0;
    Y = 0;
    Z = 0;
    W = 0;
}
template<typename T>
Vector4<T>::Vector4(T X, T Y, T Z, T W)
{
   this->X = X;
   this->Y = Y;
   this->Z = Z;
   this->W = W;
}
template<typename T>
Vector4<T>::~Vector4()
{}
template<typename T>
Vector4<T> operator*(const Vector4<T>& l, const Vector4<T>& r)
{
     return(Vector4<T>(l.X * r.X, l.Y * r.Y, l.Z * r.Z, l.W * r.W));
}
int main()
{
    Vector4<float> a, b;
    a = a * b;
}

另一个解决方案是将整个operator*模板作为朋友,而不是单个专业化:

template<typename U>
friend Vector4<U> operator*(Vector4<U> const&, Vector4<U> const&);

我能够将代码更改为非友好版本:

  1. 类定义中的代码:

    Vector4<T> operator*(Vector4<T> const & r);
    
  2. 操作员代码*。它被更改为只接受一个参数。

    template<typename T>
    Vector4<T> Vector4<T>::operator*(Vector4<T> const &r)
    {
         return(Vector4<T>(this->X * r.X, this->Y * r.Y, this->Z * r.Z, this->W * r.W));
    }
    

首先从.h文件中的operator*声明中删除friend

它应该是这样的:

Vector4<T> operator*(const Vector4<T>& r);

然后在.inl文件中,运算符*应该如下所示:

Vector4<T> Vector4<T>::operator*(const Vector4<T>& r){
     return(Vector4<T>(X * r.X, Y * r.Y, Z * r.Z, W * r.W));
}

运算符左侧的操作数称为此成员函数。

相关文章: