编辑:添加了更多的代码/其他文件以及编译错误
所以我很难弄清楚如何为我的嵌套类实现构造函数。
这是我的.h文件

//--------------------------------------------------------------------
//
//  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

和我的.cpp的一部分(逐渐使我发疯的那一部分)
#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;
}


这是用于分配的,并且提供了头文件以及其他一些文件,并且无法修改。在此之前,我从未见过/尝试过嵌套类,并且在查找有关该主题的相关信息时遇到了一些真正的困难。我发现的所有内容都在非模板类上,尽管它应该非常相似,并且/或者没有说明如何在单独的文件中实现它。从我所看到的,我的尝试是朝着正确的方向进行的,但是还没有能够对其进行编译。我试过了
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)

以及我无法想到的其他人。如果有人可以识别我的错误并向我解释错误,我将不胜感激。提前致谢!
我也包括stack.h文件,其他文件是主文件和配置文件。
//--------------------------------------------------------------------
//
//   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

这些是我从原始代码中得到的错误:
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;
                            ^~~~~

以下是不均匀建议引起的错误
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);```

最佳答案

template<class DataType>
StackLinked<DataType>::StackNode::StackNode(const DataType& nodeData, StackNode* nextPtr)
template<class DataType>引入了名称为DataType的模板参数,该参数将是任意类型。
StackLinked<DataType>指的是StackLinked的特化,以DataType作为模板参数。
StackLinked<DataType>::StackNode指的是StackNode内的嵌套类StackLinked<DataType>,它不是类模板,因此它不会获得任何模板参数列表(<...>)。
StackLinked<DataType>::StackNode::StackNodeStackNode中的StackLinked<DataType>的构造函数。

其余的是与您在类定义中的声明匹配的参数列表。

就是说,如注释中所述,通常需要将类模板成员函数的定义(以及其嵌套类的成员函数)放入头文件中。

关于c++ - C++在实现模板化嵌套类时遇到麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58576640/

10-16 18:03