【问题标题】:Invalid operands to binary expression while implementing vector [closed]实现向量时二进制表达式的无效操作数[关闭]
【发布时间】:2017-06-03 03:47:33
【问题描述】:

在一个任务中,我被要求创建自己的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])

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

【问题讨论】:

  • *URGENT*---> 要求 DV。
  • 欢迎来到 Stack Overflow!很抱歉,但您的紧迫性与人们投入时间无关。请展示您迄今为止的研究/调试工作。请先阅读How to Ask页面。
  • this[j] 并没有按照你的想法去做。
  • @SouravGhosh 关于紧急情况我深表歉意,感谢您的帮助,这是我第一次在这里提问,我编辑了

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


【解决方案1】:

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

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

【讨论】:

  • 首先this 不存在,因为sort 不是类的方法。
  • @Slava 除非您看到声明它的上下文,否则您无法知道这一点。我也没有看到上下文,但根据错误消息,*this 似乎是mathVector&lt;double&gt; 类型。
  • 提供了上下文,独立函数sort的来源在那里。
  • @Slava 我查看了我的水晶球,发现提供的代码不完整,并且您被 OP 误导了。但是,我理解您的困惑;这个问题质量不高。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-11
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
相关资源
最近更新 更多