简体   繁体   中英

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. 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. I've tried adding the "inline" keyword as per this solution and I get the same error.

/* 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 .

Note that you also need to lose the trailing <T> on both constructor and destructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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