Reputation: 25
Question:
I am trying to implement "template" in my Queue classes. I am getting errors. I am not sure if that is syntax or my implementation of "templates" has issue. Without "template", my program runs perfectly.
Question: I am not sure if the "template" implementation is the way it should be. Appreciate the comments and feedbacks.
This is SNode class
#include <string>
using namespace std;
template <class T>
class SNode {
private:
int elem;
T* next;
explicit SNode();
//friend class SQueue<T>;
};
template <class T>
SNode<T>::SNode() : elem(" "), next(nullptr) {}
This is my SQueue class which
#include <string>
#include "SNode.h"
using namespace std;
template <class T>
class SQueue {
public:
SQueue();
void enqueue(T);
void dequeue();
void print();
private:
T* front;
T* end;
};
template <class T>
SQueue<T>::SQueue() : front(NULL), end(NULL) {}
template <class T>
void SQueue<T>::enqueue(T e) {
T* np = new T();
np->elem = e;
np->next = NULL;
if (front == NULL && end == NULL) {
front = end = np;
return;
}
end->next = np;
end = np;
}
template <class T>
void SQueue<T>::dequeue() {
T* np = front;
if (front == NULL) {
cout << "The queue is empty-1!" << endl;
return;
}
front = front->next;
delete np;
}
template <class T>
void SQueue<T>::print() {
T* np = front;
if (front == NULL) {
cout << "The queue is empty-2!" << endl;
return;
}
for (T* temp = front; temp != NULL; temp = temp->next) {
if (temp != front) {
cout << " <- ";
}
cout << temp->elem;
}
cout << " [Queue: FIFO- First In First Out]" << endl;
}
This is my main/ Test file.
#include <iostream>
#include "SQueue.h"
using namespace std;
int main() {
typedef SQueue<int> SQueue;
SQueue SQ;
cout << "Queue 1: ";
SQ.enqueue(1);
SQ.enqueue(2);
SQ.enqueue(3);
SQ.enqueue(3);
SQ.print();
SQ.dequeue();
SQ.dequeue();
SQ.print();
return 0;
}
ERROR MESSAGE
Severity Code Description Project File Line Suppression State
Error C2227 left of '->elem' must point to class/struct/union/generic type DeleteLinkedList SQueue.h 24
Upvotes: 0
Views: 73
Reputation: 6471
First, your node...
//...
template <class T> // I assume T is the as-yet-unknown type stored in the node.
class SNode {
private:
int elem; // Why int ??
T* next; // Why T*
// Should be:
T elem; // The user data
SNode<T>* next; // the next node.
explicit SNode(); // Why explicit? Do you have a _very_ good reason for that?
// Should read:
SNode() : elem(T()), next(nullptr) {}
friend class SQueue<T>;
};
Now, you are delcaring a class with a private default construtor... That's a very strong indication that SNode should be declared within SQueue, as in:
template<class T>
class SQueue
{
public:
// Ideally, this inner class should be declared 'private', and SQueue
// should declare some form of iterator to hide these details from
// the caller... It's best to get everything else running first, and
// add this complexity on a solid base.
//
struct SNode // all members are public.
{
SNode() : elem(T()), next(nullptr) {}
T elem;
SNode* next;
};
public:
// define your constructors and access functions..
private:
SNode* front; // the queue stores a single-linked list of nodes.
// Only one pointer is necessary to do that.
// UNLESS you want to create a list that's optimized for
// appending elements at the end. But optimizations should be
// done after the unoptimized code has been tested and proven
// to work flawlessly.
};
Upvotes: 1