我試圖使用模板,並且我總是使用模板進行noob。一旦我開始,我會收到一些錯誤,我不明白這是什麼意思? 所以文本是在這裏,我要做的事情:模板 - 無法將函數定義與現有聲明相匹配C++
編寫一個基於模板的類來實現一組項目。該類應該允許 用戶到 a。將新項目添加到該集合。 b。獲取集合中的項目數量。 c。獲取一個指向包含集合中每個項目的動態創建的數組的指針。這個函數的調用者負責解除分配內存。
的錯誤是:
項目::輸出「:無法定義的功能匹配到一個現有的聲明
項目::在」:無法定義的功能匹配到一個現有的聲明
添加「:是不是成員」項
我的代碼是在這裏:
#include <iostream>
using namespace std;
template<class T>
class Item {
private:
Item();
~Item();
void Add(T item);
int get();
void output(T array);
bool in(T item);
T *array;
int element;
int size;
};
template<class T>
Item<T>::Item()
{
element = 0;
size = 10;
array = new T[size];
}
template<class T>
Item<T>::~Item()
{
delete[] array;
}
template<class T>
void Item<T>::add(T item)
{
if (in() == false)
{
size++;
array[size] = Item;
}
}
template<class T>
void Item<T>::in(T item)
{
for (int i = 0; i < size; i++)
{
if (array[i] == Item)
{
return true;
}
else
{
return false;
}
}
}
template<class T>
int Item<T>::get()
{
return element;
}
template<class T>
void Item<T>::output()
{
for (int i = 0; i < size; i++)
{
cout << array[i] << endl;
}
}
int main()
{
system("pause");
return 0;
}
是否'無效輸出(T數組);''匹配模板 無效項目 ::輸出()'? –
NathanOliver
是的,我看到這個錯誤,我修好了 – xerror