如何在使用类模板时删除数组中的项?

时间:2014-07-30 19:15:56

标签: c++ arrays memory-management class-template

我正在尝试从Remove函数中删除“GoodBye”,然后打印缺少它的列表。

我收到一条错误说:

  

错误1错误C2440:'删除':无法从'std :: string'转换为'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再见了。 word将保存已删除的副本。

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

将“GoodBye”和其他单词添加到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将从GoodBye

中删除my_String
    my_String.Remove("GoodBye");

    my_String.Print();

    return 0;
}

2 个答案:

答案 0 :(得分:0)

问题是您的Remove函数不应该发出对delete的任何调用。它应该做的是将元素“向上”移动一个并减少count成员变量。这实际上从数组中“删除”了该项。

要向上移动元素,可以编写一个循环,用元素i替换元素i+1,然后在要删除的项目处开始循环。

答案 1 :(得分:0)

但是,您的数组是动态分配的,您不能只在它的特定元素上调用delete。这是一个连续的内存块。你可以做@PaulMcKenzie所说的 - 找到匹配作为参数传递给Remove函数的元素的元素,然后转移到左边剩下的数组元素,然后减少count成员变量。我解决了它,但由于这是家庭作业发布,这不是明智之举。这是我的一个非常奇怪的伪代码。我希望你理解这个概念。

//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 

然后,由于递减count,最后一个元素将不会被打印 在c ++中,对于动态数组std::vector是可行的。