Syntax for constructor in template class

28,667

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.

Share:
28,667
xst
Author by

xst

studied CompSci and Psych at CSULB work experience in web development (full-stack) Java, C#/.NET, PHP, Groovy, Sass/CSS, client-side JS strongly prefer Functional/High-Level programming, but will resort to C / Assembly if necessary Erlang, Haskel, F#, Ruby, Coffeescript (not a big fan of lisp-type languages) love open source software but i'm no rms Github BitBucket use OSX, Windows, Linux (Ubuntu)

Updated on January 21, 2020

Comments

  • xst
    xst over 4 years

    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