-1
我目前在C++中學習考試。錯誤:「使用類模板's_array'需要模板參數」是什麼意思?
我在模板上做了一些練習題,並且變得完全卡住了,我檢查了我的代碼,它遵循解決方案,但是這個錯誤一直在彈出。我不確定我是如何在錯誤的arguements傳遞(這是什麼,我相信這個問題是。
代碼如下所列,任何幫助是極大的讚賞
儀
int main(){
s_array array(10);
array[5] = 5; //inbound access
cout << array[5] << endl;
array[-1] = 2;
cout << array[15];
return 0;
}
頭,類和模板:
template <typename T>
class s_array {
public:
s_array(int size);
~s_array();
T &operator[](int i);
private:
int size;
T* data;
};
template <typename T>
s_array<T>::s_array(int size) : size(size)
{
/*
* If the size of the array is greater than zero
* A new array is created at the value of size
*/
if(size > 0) data = new T[size];
else{
std::cout << "Invalid array" << endl;
exit(1);
}
}
template <typename T>
s_array<T>::~s_array()
{
delete [] data;
}
/*
* Safety feature for the array going out of bounds
*/
template <typename T>
T& s_array<T>::operator[](int i)
{
if(i < 0 || i >= size){
std::cout << "index" << i << "is out of bounds" << endl;
exit(1);
}
return data[i];
}
檢查。 – Rakete1111