Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板=找不到运算符_C++_Templates_Operator Overloading - Fatal编程技术网

C++ 模板=找不到运算符

C++ 模板=找不到运算符,c++,templates,operator-overloading,C++,Templates,Operator Overloading,我想创建模板操作符=,但它不起作用 class A { public: template<class T> A& A::operator=(const T& obj) { return *this; } }; 但这会产生错误对于类定义中的成员函数定义,您不需要A:: class A { public: template<class T> A& operator=(const T&

我想创建模板操作符=,但它不起作用

class A
{
public:

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

但这会产生错误

对于类定义中的成员函数定义,您不需要
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;
}
A类
{
公众:
模板
A和运算符=(常量T和对象)
{
归还*这个;
}
};

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

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;
}
A类
{
公众:
模板
A&运算符=(常量T&obj);
};
模板
A&A::运算符=(常量T&obj)
{
归还*这个;
}

main()
应返回
int
。您收到的错误消息是什么?在我的g++(版本6.4.0)上,我从代码中得到的唯一问题是:
error:extra-qualification'A::'On member'operator='[-fppermissive]A&A::operator=(const T&obj)
。从操作符定义中删除
A::
解决了此问题。另外,正如@O'Neil所指出的,
main
应该返回一个值。@O'Neil@MxNx
main
总是返回一个int,但main主体中的return语句不是强制性的:如果控件从main的复合语句的末尾流出,效果相当于使用操作数0的返回。@Oliv return语句确实是可选的,但是返回类型是
int
,而不是
void
。谢谢,我试图在类之外定义运算符,但这不起作用。@demanut我添加了这两种情况。