开发者

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

开发者 https://www.devze.com 2023-03-12 18:54 出处:网络
This problem arose when trying to compile a sample program from Stroustrup\'s Programming Principles and Practice usin开发者_JAVA技巧g C++.I\'m on chapter 12, where he begins using FLTK.I\'m getting c

This problem arose when trying to compile a sample program from Stroustrup's Programming Principles and Practice usin开发者_JAVA技巧g C++. I'm on chapter 12, where he begins using FLTK. I'm getting compiler errors in the graphics and GUI support code. Specifically Graph.h line 140 and 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&);  
};  

The complete headers and related graphics support code can be found here:

http://www.stroustrup.com/Programming/Graphics/

In addition to a code fix, could someone please shed some light on what is going on here in plain English. I've only just begun studying templates, so I have some vague idea, but it's still out of reach. Thanks a million,


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

The parameter type should be Vector_ref, not Vector. There is a typo.

Vector_ref& operator=(const vector&);  

And here the parameter should be vector<T>. You forgot to mention the type argument.

But reading the comment, I believe its a typo as well. You didn't mean vector<T> either. You mean these:

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

In C++0x, you can do the following to prevent copying:

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

精彩评论

暂无评论...
验证码 换一张
取 消