Private variable error in C++

Asked

Viewed 123 times

1

I can’t understand why I’m making this mistake when compiling my code:

In file included from test_GenericArray.cpp:2:0: Genericarray. h: In Function ːStd::ostream& Operator<<(Std::ostream&, const Genericarray&)': Genericarray. h:43:7: error: ?int Genericarray::length' is private int length;

I have 3 files, which are below:

Genericarray. h:

 #include <iostream>

const int MAX_ITEMS = 6;

class GenericArray {
 public:

  // Constructor
  GenericArray(); // Inicializa o nosso objeto com uma lista vazia.

  // Observers
  bool isFull() const; 
  int  getLength() const;

  // Recebe um ponteiro para um elemento do tipo int. Esse
  // elemento int que recebemos por parametro tem uma
  // chave. Usamos essa chave para buscar um elemento em nossa lista e
  // retornar esse elemento, caso ele exista.

  // Se o elemento nao estiver na lista, entao nao precisa alterar o
  // parametro item, basta avisar na variavel found que o elemento nao
  // foi achado.
  void retrieveItem(int& item, bool& found);

  // Insere o elemento na lista em uma posicao arbitraria no caso do
  // Unsorted e na posicao correta no caso do Sorted.
  void insertItem(int item); 

  // Remove uma ocorrencia do elemento na lista.
  void deleteItem(int item); // Nos nao sabemos qual a chave da
                  // classe int, entao precisamos
                  // de um objeto para saber se os
                  // elementos listados sao iguais ao
                  // nosso.

  //friend std::ostream& operator<<(std::ostream& os, const GenericArray& st);

 private:
  int length;
  int info[MAX_ITEMS];
};

test_Generic_Array.cpp (file that displays the data on the screen):

#include <iostream>
#include "GenericArray.h" 

using namespace std;


ostream& operator<<(ostream& os, const GenericArray& array){
  for (int i = 0; i < array.length; i++) {
    os << array.info[i];
  }
  os << endl;
  return os;
}

int main(){
  GenericArray unsorted;

  int elements[4] = {5, 7, 6, 9};

  for (int i = 0; i <= 3; i++) {
    int item(elements[i]);
    unsorted.insertItem(item);
  }

  cout << unsorted;

  int item(7);
  bool     found = false;

  unsorted.retrieveItem(item, found);
  cout << item << endl;

  unsorted.deleteItem(item);
  cout << unsorted;
  cout << "Fim" << endl;

}

Genericarray.cpp:

#include "GenericArray.h"

GenericArray::GenericArray(){
    length = 0;
}

bool GenericArray::isFull() const { //8:22
    return(length == MAX_ITEMS);
}

int GenericArray::getLength() const {
    return length;
}

void GenericArray::retrieveItem(int& item, bool& found){
    int location = 0;
    found = false;

    while((location < length) && !found ){
        if(item == info[location]){
            found = true;
            item = info[location];
        } 
        location++;
    }
}

void GenericArray::insertItem(int item){
    info[length] = item;
    length++;
}
void GenericArray::deleteItem(int item){
    int location = 0;

    while(item != info[location]){
        location++;
    }

    info[location] = info[length-1]; 
    length--;
}

1 answer

4


The line

for (int i = 0; i < array.length; i++) {

is accessing a call property length in the class, but it is declared as private and therefore cannot be accessed. The solution could be to make it public, but I think by code, the stated intention is the solution that would change to:

for (int i = 0; i < array.getLength(); i++) {

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.