Template Queue

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
#ifndef QUEUE_H
#define QUEUE_H
#include <cstddef>

template<class T>
class Queue
{
    public:
        Queue();
        bool isEmpty();
        void enqueue(T data);
        void dequeue();
     

    private:
        struct Node{
            T date;
            Node *next;
        };

        Node *frontPtr;
        Node *backPtr;
        int count;

};

#endif // QUEUE_H 



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 "Queue.h"
#include <iostream>
using namespace std;


template<class T>
Queue<T>::Queue(): frontPtr(NULL), backPtr(NULL), count(0)
{
}

template<class T>
bool Queue<T>::isEmpty(){
    return(count == 0);
}

template<class T>
void Queue<T>::enqueue(T data){
    Node *newOne = new Node;
    newOne->date = data;
    newOne->next = NULL;
    if(isEmpty()){
        frontPtr = newOne;
    }
        else{
            backPtr->next = newOne;
        }
        backPtr = newOne;
        count++;
}

template<class T>
void Queue<T>::dequeue(){
    if(isEmpty()){
        cout << "Nothing inside" << endl;
    }
        else{
            Node *temp = frontPtr;
            if(frontPtr == backPtr){
                frontPtr = NULL;
                backPtr = NULL;
            }
            else{
                frontPtr = frontPtr->next;
            }
            delete temp;
            count--;
        }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include "Queue.h"
using namespace std;


int main()
{
    Queue<int> bo;
    int x = 2;
    bo.enqueue(x);
    return 0;
}


I do not understand why I get this error as an output. Any help would be great.

obj\Debug\main.o||In function `main':|
C:Queue\main.cpp|9|undefined reference to `Queue<int>::Queue()'|
C:Queue\main.cpp|11|undefined reference to `Queue<int>::enqueue(int)'|
||=== Build finished: 2 errors, 0 warnings ===|



Even though this may not solve the problem, humor me by putting your "using namespace std;" in your header file instead of implementation file.
Last edited on
^That's not the problem, and you shouldn't be using anything in your header files anyway.

The issue that all your template definitions must be present in the *header* file, not a related .cpp file.
Baelix, As far as I know the using namespace std is not require in a .h file since I am not creating an abstract class... I tried it any way did not make a difference
Fire nailed it. Completely forgot the rules that go along with templating :P

Instead of having 2 seperate files (1 .cpp and 1 .h) put all of your implementation into your header file. I won't divulge where or how; that's easy enough to google :P
Thank you firedraco,Baelix Its weird that the template definition cannot be in .cpp file.
Last edited on
Well you can separate template implementation from declaration if you know in advance what specific instantiations of the template your program will use and place them in a .cpp file.

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.15

You can take the code you had in the OP and add this line to the end of Queue.cpp

template class Queue<int>;

and it should compile and link for you.
Last edited on
As far as I know the using namespace std is not require in a .h file since I am not creating an abstract class...
Quite a misconception you have. Namespaces are used to avoid name collision.
The standard c++ classes and functions are inside the std namespace.
If you want to use them, then you need to qualify them. By instance std::ostream.

You can use using to avoid that prefix thing. However the effect is as if the function were declared in the global namespace.
So using in a header defeats the purpose of namespaces.
Topic archived. No new replies allowed.