简体   繁体   English

模板类中构造函数的语法

[英]Syntax for constructor in template class

I am trying to create a generic circular buffer template but there is some syntax error that I cannot understand.我正在尝试创建一个通用的循环缓冲区模板,但存在一些我无法理解的语法错误。 The error is in my constructor, though it seems I've parameterized the destructor in the same way and that one works.错误出在我的构造函数中,尽管我似乎以相同的方式参数化了析构函数并且该方法有效。 I've followed the example in Stroustrup C++, and he uses a parameter before the scope resolution operator and also in the function name, just as I have.我遵循了 Stroustrup C++ 中的例子,他在作用域解析运算符之前和函数名中使用了一个参数,就像我一样。 I'm also certain there are no circular dependencies because I'm only compiling one file.我也确定没有循环依赖,因为我只编译一个文件。 Also the implementation and declarations are in the same file (CircBuf.h) and there is no corresponding .cpp file, so linking should not be an issue either.此外,实现和声明在同一个文件 (CircBuf.h) 中,并且没有相应的 .cpp 文件,因此链接也不应该成为问题。 I've tried adding the "inline" keyword as per this solution and I get the same error.我已尝试根据 解决方案添加“inline”关键字,但出现相同的错误。

/* CircBuf.h */
template<typename T> class CircBuf {
  // don't use default ctor                                                                                                                                               
  CircBuf();

  int size;
  T *data;
public:
  CircBuf(int);
  ~CircBuf();
};

template<typename T> CircBuff<T>::CircBuf<T>(int i) {
  data = new T[i];
}
template<typename T> CircBuf<T>::~CircBuf<T>() {
  delete data;
}

makefile生成文件

all:
        g++ -g -pedantic CircBuf.h -o prog

compiler-error编译器错误

CircBuf.h:13:22: error: ‘CircBuff’ does not name a type

CircBuff certainly does not name a type, the name of the type you intended is CircBuf with a single f . CircBuff当然不会命名类型,您想要的类型的名称是带有单个f CircBuf

Note that you also need to lose the trailing <T> on both constructor and destructor.请注意,您还需要丢失构造函数和析构函数的尾随<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM