实现 vector 时二进制表达式的操作数无效

Invalid operands to binary expression while implementing vector

本文关键字:操作数 无效 表达式 二进制 vector 实现      更新时间:2023-10-16

在一次同义中,我被要求创建自己的Vector<T>Mathvector<T>(继承自向量)和Polynomial类类型。我收到以下错误,无法弄清楚原因。

MathVector.h:37:32: error: invalid operands to binary expression ('mathVector<double>' and 'mathVector<double>')
                if (this[j]>this[j+1])

Ihe 排序函数位于 "mathVector.h" 中,其目标是按升序或降序对向量进行排序。

这是"MathVector.h"的错误部分:

void sort(int index) {
        int i,j;
        int n=this->get_size();
        if (index==1) {
            for (i=0; i<n-1; i++)
                for (j=0; j<n-i-1; j++) {
                    if (this[j]>this[j+1]) {
                        T temp;
                        temp=this[j+1];
                        this[j+1]=this[j];
                        this[j]=temp;
                    }
                }
        }
        else {
            for (i=0; i<n-1; i++)
                for (j=0; j<n-i-1; j++) {
                    if (this[j]<this[j+1]) {
                        T temp;
                        temp=this[j+1];
                        this[j+1]=this[j];
                        this[j]=temp;
                    }
                }
        }
        return;
    }

这是"vector.h":

template<class T>
class Vector {
private:
    int _size;
    int _capacity;
    T *_data;
    static T *allocate(int size) {
        return static_cast<T *>(malloc(sizeof(T) * size));
    }
    static void copyRange(T *begin, T *end, T *dest) {
        while (begin != end) {
            new((void *) dest) T(*begin);
            ++begin;
            ++dest;
        }
    }
    static void deleteRange(T *begin, T *end) {
        while (begin != end) {
            begin->~T();
            ++begin;
        }
    }
public:
    Vector() {
        _size = 0;
        _capacity = 0;
        _data = 0;
    }
    ~Vector() {
        deleteRange(_data, _data + _size);
        free(_data);
    }
    Vector(const Vector &obj) {
        this->_size = obj.get_size();
        this->_data = obj.get_data();
        this->_capacity = obj.get_capacity();
    }
    void insert(const T &value) {
        if (_size != _capacity) {
            new((void *) (_data + _size)) T(value);
            ++_size;
            return;
        }
        int newCapacity;
        if (_capacity == 0) { newCapacity = 1; }
        else (newCapacity = _capacity * 2);
        T *newData = allocate(newCapacity);
        copyRange(_data, _data + _size, newData);
        new((void *) (newData + _size)) T(value);
        deleteRange(_data, _data + _size);
        free(_data);
        _data = newData;
        _capacity = newCapacity;
        ++_size;
    }
    void resize(int index) {
        if (index == _capacity) { return; }
        else if (index > _capacity) { _capacity = index; }
        else {
            _capacity = index;
            if (index < _size) {
                deleteRange(_data + index, _data + _size);
                _size = index;
            }
        }
    }
    T &operator[](int index) {
        T empty;
        if ((index < 0) || (index >= _size)) {
            cout<<"Wrong Index";
            return empty;
        }
        return _data[index];
    }
    const T &
    operator[](int index) const {
        T empty;
        if ((index < 0) || (index >= _size)) {
            cout<<"Wrong Index";
            return empty;
        } else return _data[index];
    }
    Vector &operator=(const Vector &other) {
        this->_size = other.get_size();
        this->_data = other.get_data();
        this->_capacity = other.get_capacity();
        return *this;
    }

    friend ostream &operator<<(ostream &os, const Vector &other) {
        os << "Size: " << other._size << " | Capacity: " << other._capacity << " | ";
        int i;
        for (i = 0; i < other._size; i++) {
            os << other[i] << ",";
        }
        return os;
    }
    T *begin() const {
        return _data;
    }
    T *end() const {
        return _data + _size;
    }
    int get_size() const {
        return _size;
    }
    T* get_data() const {
        return _data;
    }
    int get_capacity() const {
        return _capacity;
    }
};
this[j]几乎

不是正确的做法。只有当*this恰好是数组中的一个子对象,并且它后面至少有j个兄弟姐妹时,它才是正确的。 this[j]相当于*(this + j)。如您所见,它在 *this 之后取消引用指向j个同级的指针。

我怀疑,您打算通过调用 Vector::operator[] 来访问缓冲区的元素。您可以通过首先取消引用指针来做到这一点:(*this)[j] .