实现 vector 时二进制表达式的操作数无效
Invalid operands to binary expression while implementing vector
在一次同义中,我被要求创建自己的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]
.
相关文章:
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 二进制表达式的操作数无效 - 使用 for 和迭代器
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 错误:二进制'operator*' 'float'和'float[0]'类型的操作数无效
- c++ 错误:二进制"运算符+"类型"矩阵*"和"矩阵*"的操作数无效
- 二进制表达式的操作数无效?
- 二进制表达式的操作数无效("常量字符*"和"常量字符*")
- 没有变量声明为函数,但错误:二进制表达式的操作数无效
- 为什么映射迭代器显示基本操作数无效错误
- 如何修复此错误:二进制表达式的操作数无效("std::vector<double>"和"double")?
- 错误:二进制表达式的操作数无效(映射使用查找函数错误)
- C++:"错误:类型'const char*'和'const char [28]'的操作数无效到二进制'ope
- 错误:使用索引寻址和 Clang 的指令的操作数无效
- 错误:二进制'operator*' 'int [1]'和'float'类型的操作数无效
- 派生类的重载<<运算符表示操作数无效
- 错误:C++ 中二进制"operator%"类型"float"和"int"的操作数无效
- 实现 vector 时二进制表达式的操作数无效
- 提升multi_index示例:错误:二进制表达式的操作数无效
- 字符和常量字符[2] 类型的操作数无效到二进制运算符
- 二进制表达式的操作数无效("无线电设备"和"常量无线电设备")