2011-06-13 22 views
0

当试图从Stroustrup的编程原理和实践中使用C++编译示例程序时出现此问题。我在第12章,他开始使用FLTK。我在图形和GUI支持代码中收到编译器错误。具体Graph.h线140和141:无效使用模板名'x'没有参数列表

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/

除了代码修复,可能有人请上一些线索这里用简单的英语发生了什么。我刚刚开始学习模板,所以我有一些模糊的想法,但仍然遥遥无期。万分感谢,

回答

7
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 
+0

哇,谢谢纳瓦兹!你疯狂的知识渊博。多谢。 – 2011-06-13 12:52:07

相关问题