简体   繁体   中英

request for member * in *, which is of non-class type *

I'm trying to make a dynamic array class in C++ but I'm getting some pointer errors, eg. on line 23 I need to change pointer value (reference number) to the value of this.first , which is also an pointer - pointer and this.first should point to the same value.

#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;
    }
};

The variable this is a pointer , and so you need to use this->first to access members of this .

The same goes for all pointers to structures, you need to use the -> operator instead of the . operator.

You can of course use the . operator, and you're very close but the pointer dereference operator * have lower precedence than the . operator, so you actually try to dereference the next element in *point.next . You need parentheses like (*point).next . However it's easier to use the -> operator instead like point->next .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM