How to delete an item in an array while using a Class Template?
-
21-12-2019 - |
Question
I'm trying to delete "GoodBye" from the Remove
function and then print a list with it missing.
I'm getting an error saying:
Error 1 error C2440: 'delete' : cannot convert from 'std::string' to 'void*
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
template <class New_Type>
class Array_Class
{
public:
Array_Class();
~Array_Class();
void Add(New_Type item);
int Search(New_Type item);
void Remove(New_Type item);
void Print();
private:
New_Type *A;
New_Type word;
int count;
};
template <class New_Type>
Array_Class<New_Type>::Array_Class()
{
cout << "You are inside the default constructor.\n";
cout << "New_Type has a size of " << sizeof(New_Type) << " bytes\n\n";
count = 0;
A = new New_Type[SIZE];
}
template <class New_Type>
Array_Class<New_Type>::~Array_Class()
{
cout << "The Destructor has been called.\n\n";
delete[] A;
count = 0;
A = 0;
}
template <class New_Type>
void Array_Class<New_Type>::Add(New_Type item)
{
if (count<SIZE)
{
A[count++] = item;
}
else
{
cout << "The array is full.\n";
}
}
template <class New_Type>
int Array_Class<New_Type>::Search(New_Type item)
{
int i;
for (i = 0; i<count; i++)
{
if (item == A[i])
{
return i;
}
}
return -1;
}
item
is Goodbye. word
will save the copy that gets deleted.
template <class New_Type>
void Array_Class<New_Type>::Remove(New_Type item)
{
int i;
word = item;
for (i = 0; i < count; i++)
{
if (item == A[i])
{
delete A[i];
}
}
}
template <class New_Type>
void Array_Class<New_Type>::Print()
{
int i;
for (i = 0; i<count; i++)
{
cout << "A[" << i << "] = " << A[i] << endl;
}
}
The main function which will add "GoodBye" and other words to my_String
.
int main()
{
Array_Class<string> my_String;
Array_Class<int> my_Ints;
Array_Class<char> my_Chars;
my_String.Add("Hello");
my_String.Add("GoodBye");
my_String.Add("ComeHere");
my_String.Add("SayNo");
my_Chars.Add('a');
my_Chars.Add('b');
my_Chars.Add('c');
my_Chars.Add('d');
my_Chars.Add('e');
my_Chars.Add('f');
my_Chars.Add('g');
my_String.Print();
my_Ints.Print();
my_Chars.Print();
cout << endl;
my_String.Search("Hello");
my_String.Search("SayNo");
my_String.Remove
will remove GoodBye
from my_String
:
my_String.Remove("GoodBye");
my_String.Print();
return 0;
}
La solution
The problem is that your Remove
function should not be issuing any calls to delete
. What it should be doing is shifting the elements "up" by one and decreasing the count
member variable. This in effect "removes" the item from the Array.
To shift the elements up, you write a loop where you replace element i
with element i+1
, where you start the loop at the item you want to remove.
Autres conseils
However your array is dynamically allocated, you can't just call delete
on particular elements of it. This is a contiguous block of memory. You can do what @PaulMcKenzie said - To find element that matches element passed as an argument to Remove
function and then shift to the left remaining array elements, then decrease count
member variable. I solved it, but since this is homework posting it wouldn't be wise. Here's a very weird pseudo-code of mine. I hope you understand that concept.
//array elements : Hello, GoodBye, ComeHere, SayNo
my_String.Remove("GoodBye");
// found index of element to remove = 1;
// decrement count
// loop from saved index through count-1:
// A[i] = A[i+1];
// There will be two iterations of this loop. here's how array would look like:
// 1st: array elements : Hello, ComeHere, ComeHere, SayNo
// 2nd: array elements : Hello, ComeHere, SayNo, SayNo
Then, because of decrementing count
, last element won't be printed.
And in c++, for dynamic arrays std::vector
is way to go.