I am creating a dynamic Que in C++ with templates for further practice. Most important functions seem to work just fine except for last 2 I have defined - void functions with no arguments.
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class DynamicQueue
{
private:
// Structure for the queue nodes
struct QueueNode
{
T value; // Value in a node
QueueNode *next; // Pointer to the next node
};
QueueNode *top; // The top of the queue
QueueNode *bottom; // The bottom of the queue
int numItems; // Number of items in the queue
public:
DynamicQueue() {
top = nullptr;
bottom = nullptr;
numItems = 0;
}
~DynamicQueue() {
delete[] top;
delete[] bottom;
}
template <typename T>
bool isEmpty() const {
bool empty;
if (numItems > 0)
empty = false;
else
empty = true;
}
template <typename T>
void display() const {
QueueNode* temp = top;
while (temp != nullptr) {
cout << temp->value << endl;
temp=temp->next;
}
}
template <typename T>
void clear() {
T temp;
while (!isEmpty()) {
dequeue(data);
}
}
};
There is an enque and a deque functions that I did not include in the code as they are working just fine. The main function looks kind of like:
int main() {
string str;
int number;
DynamicQueue<string> strQ;
DynamicQueue<int> intQ;
strQ.enqueue("Word number 1");
strQ.enqueue("2");
strQ.dequeue("2");
strQ.display();
intQ.enqueue(1);
intQ.enqueue(2);
int placehold;
intQ.dequeue(placehold);
intQ.display();
strQ.clear();
intQ.clear();
getchar();
return 0;
}
Compiler produces the following error:
error C2672: 'DynamicQueue::display': no matching overloaded function found
error C2783: 'void DynamicQueue::display(void) const': could not deduce template argument for 'T'
note: see declaration of 'DynamicQueue::display'
I understand that the issue is somewhere in me doing the templates wrong. But I dont seem to understand the correct way of doing it.
(There are the same issues for the clear() function)
You seem to have a template shadowing issue because you are redeclaring a template T over your class methods definition, which is in your case redundant.
It would have been necessary if you had implemented your methods outside your class definition.
Inside class definition:
void clear() {
// Stuff
}
Outside:
template <typename T>
void DynamicQueue<T>::clear() {
// Stuff
}
The syntax you initially used is a new template declaration specific for a method, for example to enqueue it could be:
template<typename... Args>
void enqueue(Args&&... args)
{
// Create new node
QueueNode* new_node = new QueueNode();
bottom->next = new_node;
new (&(new_node->value)) T(args...);
bottom = new_node;
}
And the outside declaration:
template<typename T>
template<typename... Args>
void DynamicQueue<T>::enqueue(Args&&... args)
{
// Create new node
QueueNode* new_node = new QueueNode();
bottom->next = new_node;
new (&(new_node->value)) T(args...);
bottom = new_node;
}
but the template argument's name cannot be T or you'll have shadowing issue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.