-2

関数から「GoodBye」を削除してから、Remove欠落しているリストを印刷しようとしています。

次のようなエラーが表示されます。

エラー 1 エラー C2440: 'delete': '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;
    }
}

に「さようなら」などの単語を追加するメイン関数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から削除GoodByemy_Stringます:

    my_String.Remove("GoodBye");

    my_String.Print();

    return 0;
}
4

2 に答える 2

1

ただし、配列は動的に割り当てられますが、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場合は道のりです。

于 2014-07-30T20:12:22.913 に答える