1

I started learning OOP in C++. I try to solve a task like this:

Create a class - a list based on a one-size-fits-all array of integers. Assign a constructor, a destructor, the functions of adding an element to the top (end) of the list, selecting an element from the list by number, sorting the list, showing the elements of the list to the top and to the bottom of the list."

In the delete function, the compiler constantly knocks out the same error:

E0852 the expression must be a pointer to the full type of the object My_4_Project C:\Users\artem\source\repos\Project4\My_4_Project\Source.cpp

Here is my code:

#include <iostream>
#include <algorithm>

using namespace std;

class Array {
  private:
    int* a;
    unsigned int size;
    int b, c = 0, d;
  public:
    Array();
    Array(int s);
    ~Array();
    int& operator[](int index);
    void setarray();
    void getarray();
    void add();
    void delet();
    void sort();
};

Array::Array() {
   size = 10;
   a = new int[size];
   for (size_t i = 0; i != size; i++) {
         a[i] = 0;
   }
}
Array::Array(int s) {
   if (s > 0) {
       size = s;
       a = new int[size];
       for (size_t i = 0; i != size; i++) {
           a[i] = 0;
       }
   }
   else cout << "Size can't be negativ";
}
Array::~Array() {
   delete[]a;
}
int& Array::operator[](int index) {
    if (index <= size) {
        return a[index];
    }
}
void Array::setarray() {
    for (size_t i = 0; i != size; i++) {
          cin >> a[i];
    }
}
void Array::getarray() {
    for (size_t i = 0; i != size; i++) {
        cout << a[i] << " ";
    }
}
void Array::add()
{
     /* ? ? ? */ ;
}
void Array::delet() {
    cin >> b;
    for (int i = 0; i < size; i++)
    {
        if (b == a[i])
            c++;
        if (c > 2) delete a[i];
    }
    cout << c;
}
void Array::sort() {
    int temp;

    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (a[j] > a[j + 1]) {
               temp = a[j];
               a[j] = a[j + 1];
               a[j + 1] = temp;
            }
        }
    }
    for (int i = 0; i < size; i++) {
        cout << a[i] << " ";
    }
}

int main() {
    cout << "Enter 10 number`s massive: ";
    Array arr(10);
    arr.setarray();
    cout << endl;
    arr.getarray();
    cout << endl;
    cout << "Sorted massive: ";
    arr.sort();
    cout << endl;
    cout << "Witch symbol you wanna delete?: ";
    arr.delet();
    return 0;
}
2
  • 1
    You are trying to delete a[i], which is of type int. That is not possible, as the error correctly states, you can only delete pointers. Sadly from your limited description I am unable to fully understand what you are trying to achieve there, so I can't really help further. Commented Feb 20, 2022 at 14:49
  • You can't delete an element from an array. You can shift all elements above the index one place to the left and decrement the size but in that case you probably want your array to have a size and a capacity where size is the number of active elements and capacity is the number of elements that are allocated. Commented Feb 20, 2022 at 14:54

1 Answer 1

1

The problem is that delete does not work as you think:

  • You can delete an object that you previously created with new (new returns a pointer, and delete expect that same pointer).
  • You can delete[] something that you previously created with new[]
  • But no mixing: you cannot delete an individual element when it was part of an array created with new[]

I will not do the exercise for you but the trick is to:

  1. find the index of the duplicate element you want to get rid off,
  2. copy every elements afterwards to one index before (i.e. a[j]=a[j+1], of course, making sure that j+1<size )
  3. reduce the size by one.

So something like:

void Array::delet() {
     cin >> b;       // better put this in main() and pass it as argument
     for (int i = 0; i < size; i++)
     {
        if (b == a[i]) 
        {            // it'll be more than a single statement
            c++;
            if (c > 2) // found a duplicate 
            {         // NO delete a[i];
                ... // insert a loop to copy the next ones
                    // and reduce the size 
                ...  // EXTRA CAUTION:  in this case the next element 
                     // is again at offset i and not i++
            }
         }
     }
     cout << c;     // ok,  you can display the number of occurences counted
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.