-1

I am realizing a templated Stack structure on the base of vector in C++. I am not sure what is wrong with my code.

Stack.h

#ifndef STACK_H_
#define STACK_H_
#include <vector>
using namespace std;
template <class T>
class Stack {
public:
    Stack();
    virtual ~Stack();
    bool empty();
    int size();
    void push(T pushvar);
    T pop();
private:
    std::vector<T> container;
};
#endif /* STACK_H_ */

Stack.cpp

#include "Stack.h"
#include <vector>
//template <class T>

Stack::Stack()
    :container(0)
    {

    }

Stack::~Stack() {
    // TODO Auto-generated destructor stub
}

bool Stack::empty(){
    return (container.empty());
}

Even before calling anything from the main, Eclipse gives me several errors. But I will give an exemplary main:

#include <iostream>
#include "Stack.cpp"
using namespace std;
int main() {
    Stack<int> s;
    cout << s.empty();
    return (0);
}

The compiler returns the following errors:

Description Resource    Path    Location    Type
'container' was not declared in this scope  Stack.cpp   /PracticeCpp/src    line 23 C/C++ Problem
'template<class T> class Stack' used without template parameters    Stack.cpp   /PracticeCpp/src    line 22 C/C++ Problem
invalid use of template-name 'Stack' without an argument list   Stack.cpp   /PracticeCpp/src    line 12 C/C++ Problem
invalid use of template-name 'Stack' without an argument list   Stack.cpp   /PracticeCpp/src    line 18 C/C++ Problem
Member declaration not found    Stack.cpp   /PracticeCpp/src    line 12 Semantic Error
Member declaration not found    Stack.cpp   /PracticeCpp/src    line 18 Semantic Error
Member declaration not found    Stack.cpp   /PracticeCpp/src    line 22 Semantic Error
Method 'empty' could not be resolved    Stack.cpp   /PracticeCpp/src    line 23 Semantic Error
Symbol 'container' could not be resolved    Stack.cpp   /PracticeCpp/src    line 13 Semantic Error

I know that I haven't realized all the methods declared in the header file, but this is not my problem. Before I continue realizing them I want to understand where am I wrong?


Correction after the answers:

I followed the suggestions in the answers but still I don't get what continues to be wrong. I moved the template realizations to the header. I deleted the other unrealized methods to avoid confusion. Now my .cpp file is empty. My new header file:

#ifndef STACK_H_
#define STACK_H_
#include <vector>


template <class T>
class Stack {
private:
    std::vector<T> container;
public:
    template <typename T>
    Stack<T>::Stack() : container(0)
    {
    }

    template <class T>
    bool Stack::empty() {
         return container.empty();
    }
};


#endif /* STACK_H_ */
Veliko
  • 747
  • 1
  • 9
  • 25

2 Answers2

2

definition should be

template <typename T>
Stack<T>::Stack() : container(0)
{
}

And not in .cpp

Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

The method Stack::empty() was never declared, you declared Stack<T>::empty(). The same is true for each other constructor, operator and method. Add a template declaration to each implementation to fix this error. Notice that the error message hints at the mistake by saying "invalid use of template-name 'Stack' without an argument list".

Example :

template<class T> bool Stack<T>::empty() { 
     return container.empty(); 
}

Implementations for template methods should be included in header files. See this question.

Edit :

Regarding your newest example, you've mixed up two solutions. Try either :

#include <vector>

template <class T>
class Stack {
private:
    std::vector<T> container;
public:
    Stack() : container(0)
    {
    }

    bool empty() {
        return container.empty();
    }
};

OR

#include <vector>

template <class T>
class Stack {
private:
    std::vector<T> container;
public:
    Stack();

    bool empty();
};

template<class T>
Stack<T>::Stack() : container(0)
{
}

template<class T>
bool Stack<T>::empty() 
{
    return container.empty();
}

In the first solution, you are defining your functions within the definition of your class. The compiler knows that you are working on class Stack<T>, you mustn't remind it. In the second solution, the functions are defined outside the class. Here, you must specify which class' empty method and constructor you are defining.

Community
  • 1
  • 1
François Andrieux
  • 28,148
  • 6
  • 56
  • 87