C++ 定义模板类、运算符和迭代器时出现问题
所以我试图定义一个模板类“TwoWayVector”和“TwoWayVectorIterator”,但我遇到了很多问题。我想在TwoWayVector中定义==以返回一个引用并将const TwoWayVector作为参数,这就是我定义其他运算符的方式,g++没有抱怨过,但出于某种原因,下面的代码产生了错误C++ 定义模板类、运算符和迭代器时出现问题,c++,templates,iterator,operators,C++,Templates,Iterator,Operators,所以我试图定义一个模板类“TwoWayVector”和“TwoWayVectorIterator”,但我遇到了很多问题。我想在TwoWayVector中定义==以返回一个引用并将const TwoWayVector作为参数,这就是我定义其他运算符的方式,g++没有抱怨过,但出于某种原因,下面的代码产生了错误 TwoWayVector.cc: In member function ‘bool& TwoWayVector<T>::operator==(TwoWayVector&l
TwoWayVector.cc: In member function ‘bool& TwoWayVector<T>::operator==(TwoWayVector<T>) [with T = int]’:
Test.cc:10: instantiated from here
TwoWayVector.cc:40: error: passing ‘const TwoWayVector<int>’ as ‘this’ argument of ‘T& TwoWayVector<T>::operator[](int) [with T = int]’ discards qualifiers
Test.cc:10: instantiated from here
TwoWayVector.cc:32: warning: reference to local variable ‘result’ returned
有什么想法吗?在
操作符==
中,您调用操作符[]
,它是常量变量向量2
上的非常量
应添加运算符的可选只读版本:
const T & operator[](const int index) const;
但是您应该像这样使用对象参数的引用:
bool operator==(const TwoWayVector &vector2) const;
否则,const
关键字实际上没有多大作用,因为它只表示您的对象副本(刚刚在堆栈上为函数创建的副本)无法修改,这并不重要。(因此,解决这个问题的最简单方法是从vector2
中删除const关键字,但它并不完全正确。)
当然,不要将bool值作为引用返回,因为它引用变量
result
,一旦您离开函数,该变量将不再存在。由于缺少复制构造函数,基本上会发生崩溃。看见什么时候
在main中使用,创建了向量的副本,保存来自numbers2的指针,然后在退出表单操作符==,副本被删除,留下numbers2指向已经释放的内存的指针,它试图在main()退出时删除这些指针,这会导致错误。为什么返回bools y引用?应该引用的是函数的参数,而不是返回值。至少不在
操作符中=
和operator==
您应该查看哪些需要通过引用传递,哪些不需要。代码有很多问题。
#include <sstream>
using namespace std;
template<typename T> class TwoWayVector;
template <class T> class TwoWayVectorIterator{
public:
TwoWayVector<T>* vector;
int currentPosition;
TwoWayVectorIterator(TwoWayVector<T>& vec){
currentPosition = 0;
vector = vec;
}
TwoWayVectorIterator( int pos , TwoWayVector<T>* vec){
currentPosition = pos;
vector = vec;
}
bool& operator==(const TwoWayVectorIterator vector2){
bool contents, position;
contents = (vector == vector2) ? true : false;
position =(currentPosition == vector2->currentPosition) ? true : false;
return (contents && position);
}
bool& operator!=(const TwoWayVectorIterator vector2){
bool contents, position;
contents = (vector == vector2) ? false : true;
position=(currentPosition == vector2->currentPosition) ? false : true;
return (contents || position);
}
TwoWayVectorIterator& operator++(){
return *this;
currentPosition = (currentPosition+1);
}
TwoWayVectorIterator& operator++(int){
currentPosition = (currentPosition+1);
return *this;
}
TwoWayVectorIterator& operator=(TwoWayVectorIterator* vector2){
&vector = vector2;
currentPosition = vector2->currentPosition;
return *this;
}
TwoWayVectorIterator& operator+(int n){
currentPosition = currentPosition+n;
return *this;
}
TwoWayVectorIterator& operator-(int n){
currentPosition = currentPosition-n;
return *this;
}
bool& operator<(TwoWayVectorIterator* vector2){
return (currentPosition<vector2->currentPosition);
}
T& operator*(){
return vector[currentPosition];
}
};
bool operator==(TwoWayVector vector2){
bool result = true;
if(capacity != vector2.capacity){
result = false;
}
if(nextFree != vector2.nextFree){
result = false;
}
for(int i=0; i<nextFree ; i++){
if(data[i] != vector2[i]){
result = false;
}
}
return result;
}
1
a.out(40908) malloc: *** error for object 0x7fe4f2c03b40: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
const T & operator[](const int index) const;
bool operator==(const TwoWayVector &vector2) const;
bool operator==(TwoWayVector vector2){