I'm coding this assignment for class where it's a stack implemented through a singly linked list (templatized) that pushes and pops 2 elements at the same time. I keep getting an error message twice that says 'node': no appropriate default constructor available. Am I supposed to add a default constructor for class node, I don't know how to approach this. I would appreciate it if someone could help. Here are my .h and .cpp files.
.h File
#ifndef _DOUBLESTACKLIST_H
#define _DOUBLESTACKLIST_H
#include <iostream>
using namespace std;
template<class ItemType>
class node
{
public:
ItemType data;
node* next;
};
template <class ItemType>
class DoubleStackList
{
private:
node<ItemType>* top;
int length;
node<ItemType>* currentPos;
public:
DoubleStackList();
//DoubleStackList(int max);
bool isEmpty();
bool isFull();
void push(ItemType item1, ItemType item2);
void pop();
ItemType topView() const;
void printStack() const;
};
#include "DoubleStackList.cpp"
#endif
.cpp File
#ifndef _DOUBLESTACKLIST_CPP
#define _DOUBLESTACKLIST_CPP
#include "DoubleStackList.h"
template<class ItemType>
DoubleStackList<ItemType>::DoubleStackList()
{
length = 0;
top = NULL;
currentPos = NULL;
}
template<class ItemType>
inline bool DoubleStackList<ItemType>::isEmpty()
{
return(length == 0); //or top == NULL
}
template<class ItemType>
inline bool DoubleStackList<ItemType>::isFull()
{
node* location;
try
{
location = new node;
delete location;
return false;
}
catch (std::bad_alloc exception)
{
return true;
}
}
template<class ItemType>
void DoubleStackList<ItemType>::push(ItemType item1, ItemType item2)
{
if (isFull())
cout << "Stack is full and no more items can be added" << endl;
else
{
// Item 1
node* temp = new node;
temp->data = item1;
temp->next = top;
top = temp;
length++;
// check if item stack is full, if it's not then another can be pushed in so it can push two at the same time
if (isFull())
cout << "No more items can be added into the stack." << endl;
else
{
// Item 2
temp = new node;
temp->data = item2;
temp->next = top;
top = temp;
length++;
}
}
}
template<class ItemType>
void DoubleStackList<ItemType>::pop()
{
if (isEmpty())
cout << "Stack is Empty and item cannot be popped" << endl;
else
{
node* temp;
temp = top;
top = top->next;
delete temp;
length--;
//we check again to see if the stack is empty if one item is popped, if it's not then we pop another so two elements get popped out of the array
if (isEmpty())
cout << "Stack is empty, no items can be popped out!" << endl;
//now we remove another by decrementing another so two elements will be popped
else
{
node* temp;
temp = top;
top = top->next;
delete temp;
length--;
}
}
}
template<class ItemType>
ItemType DoubleStackList<ItemType>::topView() const
{
if (!isEmpty()) // if list is not empty get the top's data so it shows the data
{
return top->data;
}
else // if it's empty it will display msg
{
cout << "Stack is Empty and nothing at the top" << endl;
return NULL;
}
}
template<class ItemType>
void DoubleStackList<ItemType>::printStack() const
{
node* temp;
//check if the stack is empty
if (length == 0) { //top == NULL
cout << "Stack is Empty.";
}
else {
temp = top;
while (temp != NULL) {
// print data of the node
cout << temp->data;
// assign temp link to temp
temp = temp->next;
if (temp != NULL)
cout << " -> ";
}
}
}
#endif