Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 413
xst Avatar asked Jun 07 '12 20:06

xst


1 Answers

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.

like image 145
K-ballo Avatar answered Sep 18 '22 01:09

K-ballo