模板重载=运算符在一个类中工作而无法在另一个类中编译?

时间:2013-04-09 09:34:51

标签: c++ templates compiler-errors operator-overloading

我前一段时间制作了一个小的自定义数组容器列表并且工作正常,这就是我重载其'='运算符的方式:

List<T>& operator=(const List<T>& other); //in List.h


//in List.inl
    template<typename T>
    List<T>& List<T>::operator=(const List<T>& other)
    {
        _size = other._size;

        if(_size < _capacity)
        {
            _capacity = _size;
            _AdaptCapacityChange();
        }

        for(uint i = 0; i < other._size; i++)
        {
            _data[i] = other._data[i];
        }

        return(*this);
    }

然而,现在我在另一个班级做同样的事情:

PointIndices<T>& operator=(const PointIndices<T>& point); //in PointIndices.h
//in PointIndices.inl
        template<typename T>
        PointIndicess<T>& PointIndices<T>::operator=(const PointIndicess<T>& point)
        {
            indices[0] = point.indices[0];

            return(*this);
        }

它没有突出显示PointIndices并且operator关键字保持蓝色,编译器给我:错误2错误C4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int

在这两种情况下我都正确地包含了.inl文件,PointIndices的其余方法工作正常,只有操作符给我一个问题。但是在List中,同样重载的运算符工作正常。我很困惑什么可能导致这个?

编辑:请求的测试用例:

部首:

    template<class T>
    class PointIndices
    {
        public:
            PointIndices();
            PointIndices(T P1);
            virtual ~PointIndices();

            PointIndices<T>& operator=(const PointIndices<T>& point);

            T P1() const;
            T& P1();

        protected:
            T indices[1];
    };
#include "PointIndices.inl"

INL文件:

    template<typename T>
    PointIndices<T>::PointIndices()
    {
        indices[0] = 0;
    }

    template<typename T>
    PointIndices<T>::PointIndices(T P1)
    {
        indices[0] = P1;
    }

    template<typename T>
    PointIndices<T>::~PointIndices()
    {

    }

    template<typename T>
    PointIndicess<T>& PointIndices<T>::operator=(const PointIndicess<T>& point)
    {
        indices[0] = point.indices[0];

        return(*this);
    }

    template<typename T>
    T PointIndices<T>::P1() const
    {
        return(indices[0]);
    }

    template<typename T>
    T& PointIndices<T>::P1()
    {
        return(indices[0]);
    }

1 个答案:

答案 0 :(得分:4)

您声明了一个类模板PointIndices,但在函数定义中拼错了它:

template<typename T>
PointIndicess<T>& PointIndices<T>::operator=(const PointIndicess<T>& point)
//          ^ extra "s" here                                   ^ and here