c++ - 在没有参数列表的情况下无效使用模板名称 'x'

标签 c++ templates compiler-errors arguments

当尝试使用 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

关于c++ - 在没有参数列表的情况下无效使用模板名称 'x',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6330215/

相关文章:

c++ - 为什么 operator<< 不会将我的自定义类对象隐式转换为字符串

c++ - 为什么不接受使用概念的此类特化?

c - 声明如何影响逗号运算符?

c++ - 为什么在这种情况下 QCOMPARE 不返回 True?

java - 为有经验的 C++ 开发人员快速切换到 Java

c++ - 模板函数将参数函数指针指向类方法

java - Netbeans 在删除的代码中显示编译错误

go - 如果 golang 文件依赖于 Asset 函数,如何构建它

c++ - Googletest 不运行测试夹具

c++ - 我应该如何将字节数组缓冲区传递给 writefile