-1

Trying to implement a queue using a doubly-linked list from <list>, and I'm getting the following error for my print() function: error C2760: syntax error: unexpected token 'identifier', expected ';'

The code for the Queue class is:

#ifndef DLL_QUEUE
#define DLL_QUEUE
#include <list>
#include <iostream>

using namespace std;

template<class T>
class Queue {
public:
    Queue() {
    }
    void clear() {
        lst.clear();
    }
    bool isEmpty() const {
        return lst.empty();
    }
    T& front() {
        return lst.front();
    }
    T dequeue() {
        T el = lst.front();
        lst.pop_front();
        return el;
    }
    void enqueue(const T& el) {
        lst.push_back(el);
    }
    void print() {
        for (list<T>::const_iterator i = this->front(); i != this->back(); ++i)
            cout << *i;
        cout << "\n";
    }
private:
    list<T> lst;
};
#endif

The main method that is calling the class is:

#include <iostream>
#include "genQueue.h"

using namespace std;

int main() {

    //"genQueue.h"
    Queue<int> *queue1 = new Queue<int>();
    for (int k = 0; k < 100; k++)
        queue1->enqueue(k);
    queue1->print();



    system("PAUSE");
    return 0;
}
  • @GauravSehgal tried that and it didn't work. Also tried `for (list::const_iterator i = lst.front(); i != lst.back(); ++i)` and that didn't work – Robert Schwartz Oct 05 '18 at 06:02

1 Answers1

2

Let's take a good look at the print function:

void print() {
    for (list<T>::const_iterator i = this->front(); i != this->back(); ++i)
        cout << *i;
    cout << "\n";
}

You've defined a member method T& Queue<T>::front, which you attempt to call when you write this->front(). However, T& is not assignable to List<T>::const_iterator. Additionally, this->back() attempts to call a method Queue<T>::back(), which doesn't exist.

The reason these errors don't manifest until you try to call print() is because the compiler will only instantiate templated code when it needs to. It won't try to instantiate a function you don't call, since the function might meaninglessly fail to compile for some types which you don't want to use.


If the goal is to iterate over the member list's contents, you can forget about all the above complexity and use a fool-proof ranged for loop, which is much simpler:

void print() {
    for (const T& t : lst){
        cout << t;
    }
}

On another note, there is no reason to dynamically allocate the queue using new. Note that you don't delete the queue and thus have a memory leak. You should store the queue on the stack, by value, if possible, to avoid silly pointer mistakes:

Queue<int> queue1;
for (int k = 0; k < 100; k++)
    queue1.enqueue(k);
queue1.print();

If you really need the queue to exist on the heap, consider using std::unique_ptr<Queue<T>> or std::shared_ptr<Queue<T>> for pointers that automatically manage their lifetime and ownership.

Additionally, take a look at system(“pause”); - Why is it wrong?.

alter_igel
  • 6,899
  • 3
  • 21
  • 40