模板 = 找不到运算符

template = operator not found

本文关键字:运算符 找不到 模板      更新时间:2023-10-16

我想创建模板运算符=,但它不起作用;

class A
{
public:
    template<class T>
    A& A::operator=(const T& obj)
    {
        return *this;
    }
};

是的,类为空,但运算符必须使用任何类。

void main()
{
    A a;
    a = 1.3;
}

但这会产生错误

在类定义中不需要成员函数定义A::

class A
{
public:
    template<class T>
    A& operator=(const T& obj)
    {
        return *this;
    }
};

或者,您可以在类定义之外定义它。

class A
{
public:
    template<class T>
    A& operator=(const T& obj);
};
template<class T>
A& A::operator=(const T& obj)
{
    return *this;
}

相关文章: