我正在尝试在 C++ 中创建一个动态数组类,但我遇到了一些指针错误,例如。在第 23 行,我需要将pointer
值(参考号)更改为 的值this.first
,这也是一个指针 -pointer
并且this.first
应该指向相同的值。
#include <string>
#include <iostream>
using namespace std;
class ArrayMember {
public:
ArrayMember* next;
ArrayMember* prev;
int index;
int value;
ArrayMember(){}
};
class Array {
private:
ArrayMember* first;
ArrayMember* last;
int length;
public:
int get(int index){
ArrayMember* point;
if(index<length/2){
point = this.first; //error: request for member ‘first’ in ‘this’, which is of non-class type ‘Array* const’
while(*point.index != index) { // error: request for member ‘index’ in ‘point’, which is of non-class type ‘ArrayMember*’
point = *point.next; //error: request for member ‘next’ in ‘point’, which is of non-class type ‘ArrayMember*’
}
return *point.value; //error: request for member ‘value’ in ‘point’, which is of non-class type ‘ArrayMember*’
} else if(index<length) {
point = this.last; //error: request for member ‘last’ in ‘this’, which is of non-class type ‘Array* const’
while(*point.index != index){ //error: request for member ‘index’ in ‘point’, which is of non-class type ‘ArrayMember*’
point = *point.prev; //error: request for member ‘prev’ in ‘point’, which is of non-class type ‘ArrayMember*’
}
return *point.value; //error: request for member ‘value’ in ‘point’, which is of non-class type ‘ArrayMember*’
}else{
return NULL; //warning: converting to non-pointer type ‘int’ from NULL
}
}
int set(int index, int value){
//...
}
int indexOf(int value){
//...
}
Array(){
ArrayMember x = new ArrayMember;
x.index = 0;
this.first = x;
this.last = x;
}
};