32

I'm trying to learn templates in C++ and I have the following code :

#include <stack>

template<typename T>
class   myClass : public std::stack<T>{
public:
    myClass(void);
    myClass(myClass const & src);
    virtual ~myClass(void);
    myClass &   operator=(myClass const & rhs);
};

template<typename T>
myClass::myClass(void) : std::stack<T>(){
}

But I can't figure out why I get the following when I try to compile :

test.cpp:17:1: error: 'myClass' is not a class, namespace, or enumeration
myClass::myClass(void) : std::stack<T>(){
^
test.cpp:8:9: note: 'myClass' declared here
class   myClass : public std::stack<T>{
        ^
1 error generated.

It looks like the definition of the function causes the error, but I don't know why I get this error, it looks OK to me (even if I guess it's not really OK), just a syntax error perhaps?..

I compile with clang++ -Wall -Werror -Wextra -c.

What could cause this error?

vmonteco
  • 14,136
  • 15
  • 55
  • 86
  • 2
    Side note: You don't need to specify `void` if a function has no parameters, just use `myClass();`. – user657267 Jan 15 '16 at 02:18
  • 1
    @user657267 That's true, indeed, but I have to use it in my school for programming style reason (asked by my school, I took the habit). – vmonteco Jan 15 '16 at 02:22

1 Answers1

49

You need to specify the template parameter for it, since myClass is a class template.

template<typename T>
myClass<T>::myClass(void) : std::stack<T>() {
//     ^^^
}

LIVE


BTW: : std::stack<T>() seems to be redundant.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • That was it! thank you very much, that was a stupid question perhaps, my bad! Is that because the compiler needs this parameter needs to "instanciate" the class before defining its member functions? (I may be wrong though, I'm simply trying to understand the purpose of this) – vmonteco Jan 15 '16 at 02:25
  • @vmonteco I don't think so. I think the compiler just think `myClass` should be followed by `<>`, because it's a template. So said differently, the compiler can't find a non-template class named `myClass`. – songyuanyao Jan 15 '16 at 02:27
  • @songyuanyo I just found out it's also required if I pass it as a parameter, so I guess it's a part of the class definition as a type? (Perhaps it's something like **myClass** -> template and **myClass** -> class created with the template?), (BTW, what do you mean by "std::stack()" is redundant, I could do without in this case? – vmonteco Jan 15 '16 at 02:30
  • 1
    @vmonteco Yes, it's a part of the class. `: std::stack()` is redundant, because the base class will be default constructed if you don't specify anthing. It'll be useful if you want the base class to be constructed specially, sush as `: std::stack(std::vector())`. – songyuanyao Jan 15 '16 at 02:42
  • It makes sense, I get it. Thank you for these explanations! – vmonteco Jan 15 '16 at 02:47
  • Here's some reading on this: [Class template argument deduction (CTAD) (since C++17)](https://en.cppreference.com/w/cpp/language/class_template_argument_deduction). This is another case of C++ being a pain in the butt. :) – Gabriel Staples Dec 01 '20 at 00:04