2

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);```
  • The only special thing about nested classes is that they have access to private members of their surrounding classes. – n. m. could be an AI Oct 27 '19 at 04:34
  • 2
    Templates should usually be implemeneted in the header, not in a `.cpp` file. See [this post](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) for more info. – super Oct 27 '19 at 04:36
  • If you are getting errors you need to post a [mcve] which includes your source file(s) the specific error(s) in full. – n. m. could be an AI Oct 27 '19 at 04:38
  • I have a sneaking suspicion what is wrong, but for the record could you [edit your question](https://stackoverflow.com/posts/58576640/edit) and copy and paste the compiler error you get? That will help me be more or less certain. –  Oct 27 '19 at 04:46
  • @n.m. and Chipster, I added the source file associated with this project as well as the full .cpp and the errors associated with what I had posted as well as the suggestion below. super This is the way we have been doing it thus far in class and which is why I wrote it this way. I'm going to review your suggestion to try and learn more. Thanks for the help all! I am clearly new to asking questions on here so any help, about my question or otherwise would be greatly appreciated. – hapless_automaton Oct 27 '19 at 05:14
  • The help from @uneven_mark has got it allllll better. Thank you again for all your help as well. This is truly an amazing community! Thanks again everybody! – hapless_automaton Oct 27 '19 at 05:19
  • @StephenLeer (Pure) thank you comments are not that well liked here. Instead see what else you can do thank someone for an answer here: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) See also [this discussion on meta.stackexchange.com](https://meta.stackexchange.com/questions/126180/is-it-acceptable-to-write-a-thank-you-in-a-comment). – walnut Oct 27 '19 at 05:27

1 Answers1

2
template<class DataType>
StackLinked<DataType>::StackNode::StackNode(const DataType& nodeData, StackNode* nextPtr)

template<class DataType> introduces a template parameter with name DataType, which will be some arbitrary type.

StackLinked<DataType> refers to the specialization of StackLinked with DataType as template argument.

StackLinked<DataType>::StackNode refers to the nested class StackNode inside StackLinked<DataType>, which is not a class template, so it doesn't get any template argument list (<...>).

StackLinked<DataType>::StackNode::StackNode refers to the constructor of StackNode inside StackLinked<DataType>.

The rest is the parameter list matching your declaration inside the class definition.

That said, as mentioned in the comments, definitions of class template member functions (and also member functions of their nested classes) usually need to be placed into the header file.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • Thank you for the answer but your suggestion still results in errors on my end. I have added more code as well as the errors I get for both your suggestion and what I originally posted. Thanks again. – hapless_automaton Oct 27 '19 at 05:08
  • @StephenLeer No, the code I suggested as introduction of the constructor definition does not generated any error anymore. These error messages are from unrelated code that you didn't show earlier and didn't ask about. But all of these are for the same reason as I explained in this answer: `StackNode` is not a class template, it is just a class. Therefore it does not get to have any template argument list (the thing enclosed by `<` and `>`). So, e.g., `StackNode* old = other.top;` is wrong. – walnut Oct 27 '19 at 05:12
  • AHHHHHHHHHHH Thank you soooo MUCH! This whole project had me so turned around, between an uneven understanding of templates and the nested classes. – hapless_automaton Oct 27 '19 at 05:17