template help

im getting these strange errors when trying to learn templates, i appreciate any help.

ERRORS:

obj\Debug\main.o||In function `main':|
F:\C++\TemplateStack\main.cpp|9|undefined reference to `stack<std::string>::stack()'|
F:\C++\TemplateStack\main.cpp|29|undefined reference to `stack<std::string>::isfull() const'|
F:\C++\TemplateStack\main.cpp|32|undefined reference to `stack<std::string>::push(std::string const&)'|
F:\C++\TemplateStack\main.cpp|36|undefined reference to `stack<std::string>::isempty() const'|
F:\C++\TemplateStack\main.cpp|39|undefined reference to `stack<std::string>::pop(std::string&)'|
||=== Build finished: 5 errors, 0 warnings ===|



main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <cctype>
#include "stack.h"
using std::cout;
using std::cin;

int main()
{
    stack<std::string> st;      //create empty stack
    char ch;
    std::string po;
    cout << "Please enter A to add a purchase order,\n"
         << "P to process a PO, or Q to quit.\n";
    while(cin >> ch && std::toupper(ch) != 'Q')
    {
        while(cin.get() != '\n')
            continue;
        if (!std::isalpha(ch))
        {
            cout << "\a";
            continue;
        }
        switch(ch)
        {
            case 'A':
            case 'a':
                cout << "Enter a PO number to add: ";
                cin >> po;
                if(st.isfull())
                    cout << "stack already full\n";
                else
                    st.push(po);
                break;
            case 'P':
            case 'p':
                if(st.isempty())
                    cout << "stack already empty\n";
                else{
                    st.pop(po);
                    cout << "PO #" << po << " popped\n";
                    break;}
        } //switch statements
    } //main while loop
    cout << "Goodbye!\n";
    return 0;
} //int main()

stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef STACK_H
#define STACK_H
template<class Type>
class stack
{
    private:
        enum {MAX = 10};
        Type items[MAX];                //container
        int top;
    public:
        stack();
        bool isempty() const;           //test if empty
        bool isfull() const;            //test if full
        bool push(const Type & item);   //add item to stack
        bool pop(Type & item);          //pop top into item
};

#endif // STACK_H

stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "stack.h"
#include <iostream>
#include <string>
#include <cctype>
template<class Type>
stack<Type>::stack()
{
    top = 0;
}


template<class Type>
bool stack<Type>::isempty() const
{
    return top == 0;
}

template<class Type>
bool stack<Type>::isfull() const
{
    return top == MAX;
}

template<class Type>
bool stack<Type>::push(const Type & item)
{
    if ( top < MAX)
    {
        items[top++] = item;
        return true;
    }
    else
        return false;
}

template<class Type>
bool stack<Type>::pop(Type & item)
{
    if(top > 0)
    {
        item = items[--top];
        return true;
    }
    else
        return false;
}

The definition of a template class with all definitions of its member function should be placed in one header file. The problem is that during compilation of the module containing the main the compiler does not see all definitions of the template.
thanks for the fast reply, my book says something about using the export keyword, but it doesn't go into detail. how would i go about doing that? or should i just place the definitions in the same file?
The keyword export is not supported by most of compilers. And moreover this keyword is not used any more with templates in the C++ 2011 Standard.
Last edited on
thanks, i really appreciate the help!
Topic archived. No new replies allowed.