EDIT: Added more of the code/additional files as well as the compile errors So I'm having a hard time figuring out how to implement the constructor for my nested class. Here is my .h file
//--------------------------------------------------------------------
//
// StackArray.h
//
// Class declaration for the array implementation of the Stack ADT
//
//--------------------------------------------------------------------
#ifndef STACKARRAY_H
#define STACKARRAY_H
#include <stdexcept>
#include <iostream>
using namespace std;
#include "Stack.h"
template <typename DataType>
class StackLinked : public Stack<DataType> {
public:
StackLinked(int maxNumber = Stack<DataType>::MAX_STACK_SIZE);
StackLinked(const StackLinked& other);
StackLinked& operator=(const StackLinked& other);
~StackLinked();
void push(const DataType& newDataItem) throw (logic_error);
DataType pop() throw (logic_error);
void clear();
bool isEmpty() const;
bool isFull() const;
void showStructure() const;
private:
class StackNode {
public:
StackNode(const DataType& nodeData, StackNode* nextPtr);
DataType dataItem;
StackNode* next;
};
StackNode* top;
};
#endif //#ifndef STACKARRAY_H
and part of my .cpp (the part that is slowly making me crazy)
#include "StackLinked.h"
#include <iostream>
using namespace std;
template<class DataType>
StackLinked<DataType>::StackNode<DataType>::StackNode(const DataType& nodeData, StackNode* nextPtr)
{
dataItem = nodeData;
next = nextPtr;
}
template<class DataType>
StackLinked<DataType>::StackLinked(int maxNumber)
{
top = nullptr;
}
template<class DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
{
StackNode<DataType>* old = other.top;
if(isEmpty())
top = nullptr;
else{
top = new StackNode<DataType>;
top->dataItem = old->dataItem;
StackNode<DataType>* newPtr = top;
while(old != nullptr)
{
old = old->next;
DataType nextItem = old->dataItem;
StackNode<DataType>* temp = new StackNode<DataType>;
temp->dataItem = nextItem;
newPtr->next = temp;
newPtr = newPtr->next;
}
newPtr->next = nullptr;
}
}
template<class DataType>
StackLinked<DataType>::~StackLinked()
{
clear();
}
template<class DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
{
if(this != &other)
{
while(top != nullptr)
{
StackNode<DataType>* nodePtr = top;
top = top->next;
delete nodePtr;
}
top = nullptr;
top = new StackNode<DataType>;
top->dataItem = other->dataItem;
StackNode<DataType>* newPtr = top;
while(other != nullptr)
{
other = other->next;
DataType nextItem = other->dataItem;
StackNode<DataType>* temp = new StackNode<DataType>;
temp->dataItem = nextItem;
newPtr->next = temp;
newPtr = newPtr->next;
}
newPtr->next = nullptr;
}
return *this;
}
template<class DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw (logic_error)
{
if(top == nullptr)
{
top = new StackNode<DataType>;
top->dataItem = newDataItem;
}
else
{
StackNode<DataType> nodePtr = new StackNode<DataType>;
nodePtr->next = top;
nodePtr->data = newDataItem;
top = nodePtr;
}
}
template<class DataType>
DataType StackLinked<DataType>::pop() throw (logic_error)
{
if(isEmpty())
{
throw logic_error("Empty Stack");
}
else
{
StackNode<DataType> nodePtr = new StackNode<DataType>;
nodePtr = top;
top = top->next;
DataType anItem = nodePtr->dataItem;
delete nodePtr;
return anItem;
}
}
template<class DataType>
void StackLinked<DataType>::clear()
{
while(top != nullptr)
{
StackNode<DataType>* nodePtr = top;
top = top->next;
delete nodePtr;
}
}
template<class DataType>
bool StackLinked<DataType>::isEmpty() const
{
return top == nullptr;
}
template<class DataType>
bool StackLinked<DataType>::isFull() const
{
return false;
}
This is for an assignment and the header file was supplied, along with a few other files, and cannot be modified. I have never seen/tried nested classes before this and have been having some real difficulty trying to find relevant info on the topic. All of what I've found is on non templated classes, though it should be fairly similar, and/or doesn't explain how to implement it in a separate file. From what I've seen, my attempt is in the right direction but have not been able to get it compile. I've tried
class StackLinked<DataType>::StackNode<DataType>(const DataType& nodeData, StackNode* nextPtr)
class StackLinked<DataType>::StackNode(const DataType& nodeData, StackNode* nextPtr)
class StackLinked<DataType>::StackNode::StackNode(const DataType& nodeData, StackNode* nextPtr)
StackNode::StackNode(const DataType& nodeData, StackNode* nextPtr)
StackLinked::StackNode(const DataType& nodeData, StackNode* nextPtr)
and a whole slew of others that I can't think of a.t.m.. I would truly appreciate if someone could identify my error and explain it to me. Thanks in advance! I'm including the stack.h file as well, the other files are the main and a config file.
//--------------------------------------------------------------------
//
// Stack.h
//
// Class declaration of the abstract class interface to be used as
// the basis for implementations of the Stack ADT.
//
//--------------------------------------------------------------------
#ifndef STACK_H
#define STACK_H
#include <stdexcept>
#include <iostream>
using namespace std;
template <typename DataType>
class Stack {
public:
static const int MAX_STACK_SIZE = 8;
virtual ~Stack();
virtual void push(const DataType& newDataItem) throw (logic_error) = 0;
virtual DataType pop() throw (logic_error) = 0;
virtual void clear() = 0;
virtual bool isEmpty() const = 0;
virtual bool isFull() const = 0;
virtual void showStructure() const = 0;
};
template <typename DataType>
Stack<DataType>::~Stack()
// Not worth having a separate class implementation file for the destuctor
{}
#endif // #ifndef STACK_H
These are the errors I get from my original code:
sleer@DESKTOP-96LGT1D:/mnt/c/Users/steph/Desktop/cs302/projects/proj1/cs302-hw2-code-package$ g++ StackLinked.cpp StackLinked.h Stack.h
StackLinked.cpp:6:24: error: non-template ‘StackNode’ used as template
StackLinked<DataType>::StackNode<DataType>::StackNode(const DataType& nodeData, StackNode* nextPtr)
^~~~~~~~~
StackLinked.cpp:6:24: note: use ‘StackLinked<DataType>::template StackNode’ to indicate that it is a template
StackLinked.cpp:6:1: error: need ‘typename’ before ‘StackLinked<DataType>::StackNode’ because ‘StackLinked<DataType>’ is a dependent scope
StackLinked<DataType>::StackNode<DataType>::StackNode(const DataType& nodeData, StackNode* nextPtr)
^~~~~~~~~~~~~~~~~~~~~
In file included from StackLinked.h:17:0:
Stack.h:25:52: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
virtual void push(const DataType& newDataItem) throw (logic_error) = 0;
^~~~~
Stack.h:26:28: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
virtual DataType pop() throw (logic_error) = 0;
^~~~~
StackLinked.h:29:44: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
void push(const DataType& newDataItem) throw (logic_error);
^~~~~
StackLinked.h:30:20: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
DataType pop() throw (logic_error);
^~~~~
Stack.h:25:52: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
virtual void push(const DataType& newDataItem) throw (logic_error) = 0;
^~~~~
Stack.h:26:28: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
virtual DataType pop() throw (logic_error) = 0;
^~~~~
The following is the error/s that I get with the suggestion from uneven_mark
sleer@DESKTOP-96LGT1D:/mnt/c/Users/steph/Desktop/cs302/projects/proj1/cs302-hw2-code-package$ g++ StackLinked.cpp StackLinked.h
StackLinked.cpp: In copy constructor ‘StackLinked<DataType>::StackLinked(const StackLinked<DataType>&)’:
StackLinked.cpp:19:3: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType>* old = other.top;
^~~~~~~~~
StackLinked.cpp:23:15: error: ‘StackLinked<DataType>::StackNode’ is not a template
top = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp:26:5: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType>* newPtr = top;
^~~~~~~~~
StackLinked.cpp:31:7: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType>* temp = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp:31:39: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType>* temp = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp: In member function ‘StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked<DataType>&)’:
StackLinked.cpp:51:7: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType>* nodePtr = top;
^~~~~~~~~
StackLinked.cpp:56:15: error: ‘StackLinked<DataType>::StackNode’ is not a template
top = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp:59:5: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType>* newPtr = top;
^~~~~~~~~
StackLinked.cpp:64:7: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType>* temp = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp:64:39: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType>* temp = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp: At global scope:
StackLinked.cpp:74:63: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
void StackLinked<DataType>::push(const DataType& newDataItem) throw (logic_error)
^~~~~
StackLinked.cpp: In member function ‘void StackLinked<DataType>::push(const DataType&)’:
StackLinked.cpp:78:15: error: ‘StackLinked<DataType>::StackNode’ is not a template
top = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp:83:5: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType> nodePtr = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp:83:39: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType> nodePtr = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp: At global scope:
StackLinked.cpp:90:39: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
DataType StackLinked<DataType>::pop() throw (logic_error)
^~~~~
StackLinked.cpp: In member function ‘DataType StackLinked<DataType>::pop()’:
StackLinked.cpp:98:5: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType> nodePtr = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp:98:39: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType> nodePtr = new StackNode<DataType>;
^~~~~~~~~
StackLinked.cpp: In member function ‘void StackLinked<DataType>::clear()’:
StackLinked.cpp:111:5: error: ‘StackLinked<DataType>::StackNode’ is not a template
StackNode<DataType>* nodePtr = top;
^~~~~~~~~
In file included from StackLinked.h:17:0:
Stack.h:25:52: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
virtual void push(const DataType& newDataItem) throw (logic_error) = 0;
^~~~~
Stack.h:26:28: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
virtual DataType pop() throw (logic_error) = 0;
^~~~~
StackLinked.h:29:44: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
void push(const DataType& newDataItem) throw (logic_error);
^~~~~
StackLinked.h:30:20: warning: dynamic exception specifications are deprecated in C++11 [-Wdeprecated]
DataType pop() throw (logic_error);```