簡體   English   中英

在沒有參數列表的情況下無效使用模板名稱“x”

[英]invalid use of template-name 'x' without an argument list

嘗試使用 C++ 編譯 Stroustrup 的編程原理和實踐中的示例程序時出現此問題。 我在第 12 章,他開始使用 FLTK。 我在圖形和 GUI 支持代碼中遇到編譯器錯誤。 特別是 Graph.h 第 140 和 141 行:

error: invalid use of template-name 'Vector' without an argument list  
error: ISO C++ forbids declaration of 'parameter' with no type  


template<class T> class Vector_ref {  
    vector<T*> v;  
    vector<T*> owned;  
public:  
    Vector_ref() {}  
    Vector_ref(T& a) { push_back(a); }  
    Vector_ref(T& a, T& b);  
    Vector_ref(T& a, T& b, T& c);  
    Vector_ref(T* a, T* b = 0, T* c = 0, T* d = 0)  
    {  
        if (a) push_back(a);  
        if (b) push_back(b);  
        if (c) push_back(c);  
        if (d) push_back(d);  
    }  

    ~Vector_ref() { for (int i=0; i<owned.size(); ++i) delete owned[i]; }  

    void push_back(T& s) { v.push_back(&s); }  
    void push_back(T* p) { v.push_back(p); owned.push_back(p); }  

    T& operator[](int i) { return *v[i]; }  
    const T& operator[](int i) const { return *v[i]; }  

    int size() const { return v.size(); }  

private:    // prevent copying  
    Vector_ref(const Vector&);  <--- Line 140!
    Vector_ref& operator=(const vector&);  
};  

完整的標題和相關的圖形支持代碼可以在這里找到:
http://www.stroustrup.com/Programming/Graphics/

除了代碼修復之外,有人可以用簡單的英語解釋一下這里發生了什么。 我才剛剛開始研究模板,所以我有一些模糊的想法,但仍然遙不可及。 太感謝了,

Vector_ref(const Vector&);  <--- Line 140!

參數類型應該是Vector_ref ,而不是Vector 有一個錯字。

Vector_ref& operator=(const vector&);  

這里的參數應該是vector<T> 您忘記提及類型參數。

但是閱讀評論,我相信它也是一個錯字。 你也不是說vector<T> 你的意思是這些:

// prevent copying  
Vector_ref(const Vector_ref&);  
Vector_ref& operator=(const Vector_ref&); 

在 C++0x 中,您可以執行以下操作來防止復制:

// prevent copying  
Vector_ref(const Vector_ref&) = delete;            //disable copy ctor
Vector_ref& operator=(const Vector_ref&) = delete; //disable copy assignment

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM