-3

I'm having issues with this code for a bag ADT. I'm using a a pointer array made of a separate class that stores a string and an int.

ArrayBag::ArrayBag(int length){
  list = new ReceiptArray [length];
  size = length;
  n= 0;
}

int ArrayBag::getCurrentSize(){return n;};

bool ArrayBag::isEmpty() {
  if (!(n==0)){
    return 1;
    cout << "has items"<< endl;
  }
  else{
    cout << "empty" << endl;
    return 0;
  }
}

bool ArrayBag::add(string item){
  if (!(n==size)){

    (*list+n.item) = item;
    (*list+n.itemnum) = n+1;

it is the pointer arithmetic lines above that are proving difficult

    n++;
    return 1;
  }
  else{
    cout << "bag full" << endl;
    return 0;}
}

bool ArrayBag::remove(string item){
  for(int k=0;k<n;k++){

Here too

    if ((*list+k.item) ==  item){
      (*list+k.item) = (*list+n.item);


      n--;
      return true;
    }
  }
  return false;
}

void ArrayBag::clear(){n=0;}

int ArrayBag::getFrequencyOf(string item){
  int frequency = 0;
  for(int k=0;k<n;k++){
    if(*list+k.item == item){
      ++frequency;}
  }
  return frequency;
}


bool ArrayBag::contains(string item){
  for(int k=0;k<n;k++){
    if(*list+k.item == item){
      return 1;
    }
  }
  return 0;
}

The errors

  error: request for member ‘item’ in ‘((ArrayBag*)this)->ArrayBag::n’, which is of non-class type ‘int’
 (*list+n.item) = item;

I'm confused on why this is happening. I thought you could index the pointer array this way. It happens anywhere where I try to add the current size of the array to properly index the array and access the arrays member variables. Any advice is welcome

Eli D.
  • 5
  • 1
  • 4

1 Answers1

0

You have: (*list+n.item) where list is a pointer to a ReceiptArray (you should probably rename the class from ReceiptArray to Receipt but that's not your issue.)

You have two issues:

1) An order of operation issue. First *list is evaluated which returns an object of type ReceiptArray. But you need to increment your pointer before you dereference it.

2) Next you try to add n.item to this object. But n is an int and doesn't have an item member. Hence the error.

You want: (*(list+n)).item = item;

Or: (list+n)->item = item

Or simply: list[n] = item;

MFisherKDX
  • 2,840
  • 3
  • 14
  • 25