mostly_tired
mostly_tired

Reputation: 45

Using a template for a stack implementation; "name followed by :: must be a class or namespace"

stack.h

template <class DataType>
class Stack {
private:
    int * topPtr;
public:
    Stack();    //constructor; initializes top to nullptr
    void push();    //adds a node
    void pop();     //removes most recently added node
};

And stack.cpp thus far:

#include "stack.h"
//using namespace std;
template <class DataType>
Stack::Stack() {
    topPtr = nullptr;
}

It is the Stack:: that is underlined with the error "name followed by :: must be a class or namespace." If relevant, I am using VS Code.

I would just like to have a generic stack implementation, and I'm not sure why I'm getting this error? Thanks!

Upvotes: 0

Views: 116

Answers (1)

Jarod42
Jarod42

Reputation: 217293

Syntax of out of class definition is:

template <class DataType>
Stack<DataType>::Stack() : topPtr{nullptr} {}
//    ^^^^^^^^

Upvotes: 1

Related Questions