模板重载=操作符在一个类中工作,在另一个类中无法编译

Template overloaded = operator works in one class and fails to compile in another?

本文关键字:工作 另一个 编译 一个 重载 操作符      更新时间:2023-10-16

我做了一个小的自定义数组容器列表前一段时间,它工作得很好,这就是我如何重载它的'='操作符:

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和操作符关键字保持蓝色,编译器给我:错误2错误C4430:缺少类型说明符- int假设。注意:c++不支持default-int

在这两种情况下,我都正确地包含了.inl文件,PointIndices的其余方法工作得很好,只有操作符给了我一个问题。然而,在List中,相同的重载操作符可以正常工作。我很困惑,是什么引起的?

EDIT: requested testcase:

头:

    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]);
    }

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

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