实现向量时,二进制表达式的操作数无效

时间:2017-01-18 16:47:08

标签: c++ arrays c++11 vector

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

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

我的排序函数在“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;
    }
};

1 个答案:

答案 0 :(得分:2)

this[j]几乎不是正确的事情。它只能是正确的,如果*this恰好是数组中的子对象,并且后面至少有j个兄弟。 this[j]相当于*(this + j)。正如您所看到的,它会在j之后取消引用指向*this兄弟的指针。

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