我试图从Remove
函数中删除“GoodBye”,然后打印一个缺少它的列表。
我收到一条错误消息:
错误 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;
}
}
将“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;
}