[英]ERROR: What does “use of class template 's_array' requires template arguments” mean?
我目前正在學習C ++考試。
我在模板上做一些練習問題,完全陷入困境,檢查了我的代碼,它遵循了解決方案,但此錯誤不斷彈出。 我不確定我是如何提出錯誤的論點的(這就是我認為的問題所在。
下面列出了代碼,非常感謝您的幫助
測試儀
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];
}
您需要說出s_array
持有什么類型,例如,這將定義包含int
類型的數組。 在模板定義中,現在將在T
曾經被替換為的所有地方替換int
s_array<int> array(10);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.