âStackâ does not name a type

Im making a stack class using a vector and every time i compile it im getting the error âStackâ does not name a type even though i included it in my hpp file and im only using one class
Last edited on
Post the full error messages and the line(s) that gave you the errors please.
Remove #include "stack.h" from the second snippet (which I assume is stack.hpp.)
stack.hpp:4:2: error: âStackâ does not name a type
Stack<T>::Stack()
^
stack.hpp:8:2: error: âStackâ does not name a type
Stack<T>::~Stack()
^
stack.hpp:13:2: error: âStackâ does not name a type
Stack<T>& Stack<T>::operator= (const Stack <T> & rhs)
^
stack.hpp:20:2: error: âStackâ does not name a type
Stack<T>& Stack<T>::operator=(Stack<T> && rhs)
^
stack.hpp:26:12: error: expected initializer before â<â token
bool Stack<T>::empty() const
^
stack.hpp:31:12: error: expected initializer before â<â token
void Stack<T>::clear()
^
stack.hpp:36:12: error: expected initializer before â<â token
void Stack<T>::push(const T& x)
^
stack.hpp:41:12: error: expected initializer before â<â token
void Stack<T>::push(T && x)
^
stack.hpp:46:12: error: expected initializer before â<â token
void Stack<T>::pop()
^
stack.hpp:51:10: error: expected initializer before â<â token
T& Stack<T>::top()
^
stack.hpp:56:16: error: expected initializer before â<â token
const T& Stack<T>::top() const
^
stack.hpp:61:11: error: expected initializer before â<â token
int Stack<T>::size() const
^
stack.hpp:66:12: error: expected initializer before â<â token
void Stack<T>::print(std::ostream& os, char ofc)const
cire i tried that but it still gives me the same errors
Make sure you're then including stack.h and not stack.hpp in whatever source files are using it.
These were my only two file my stack.h header file and my stack.hpp my implementation and was trying to make sure it worked right before i wrote my test main program to go with it
only place i put the include the include stack.hpp was at the bottom of .h file and then included the .h file in the .hpp so that it would have all the definitions
Last edited on
These were my only two file my stack.h header file and my stack.hpp my implementation and was trying to make sure it worked right

The only way to "make sure it works right" is to include stack.h in a source file and instantiate/exercise a Stack object. Header files are not compiled on their own, and if you have included them as source files in your project or are attempting to compile them like you would a .cpp file, you're doing the wrong thing.
oh is there a specific way you have to compile hpp files with g++, this is my first time using them so did not know you had to do treat them differently than with cpp files

Last edited on
Hi,

The #include is just like one had copy / pasted the contents of the hpp file into the cpp file at that point. It's just a convenient thing for humans, so that we can separate things into different files. And that the header file can be reused for anything else that needs it.

So when one compiles all the cpp files, the hpp files are already a part of them.

Just put all of the template stuff into one hpp file, and include that in whatever cpp file needs it.

The hpp file extension means a header file with cpp code in it (as opposed to .h which is a convention for a C header), as distinct from an implementation file (function definitions) which has the cpp file extension. The header file extensions are just conventions: .h is acceptable for C++ too, but I think .hpp makes more sense.

With templates, normally all the template code* should go in a header file. This might be the point of confusion: non templated implementation (function definitions) goes in a cpp file. Is that why you had both .h and .hpp files?

*I found this:
https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl

They have a way to put template definitions into cpp files, not sure if that has other implications.
Topic archived. No new replies allowed.