-6

不久前我制作了一个小型自定义数组容器 List ,它工作正常,这就是我重载它的 '=' 运算符的方式:

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++ 不支持默认整数

在这两种情况下,我都正确地包含了 .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]);
    }
4

1 回答 1

4

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

template<typename T>
PointIndicess<T>& PointIndices<T>::operator=(const PointIndicess<T>& point)
//          ^ extra "s" here                                   ^ and here
于 2013-04-09T09:43:36.400 回答