2017-08-05 50 views
-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]; 
} 
+0

檢查。 – Rakete1111

回答

2

你需要說什麼鍵入s_array持有,例如,這將定義數組包含intŧ YPES。在模板定義,int現在到處是取代用於T是你又應該如何使用一個類模板,其中類型不能從構造函數調用來推斷

s_array<int> array(10); 
+0

謝謝你的男人!像往常一樣,我想念的是簡單的事情 –

相關問題