c ++在实现模板化嵌套时遇到问题class
c++ Having trouble implementing templated nested class
编辑:添加了更多 code/additional 文件以及编译错误
所以我很难弄清楚如何为我的嵌套 class 实现构造函数。
这是我的 .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;
}
这是一项作业,提供了头文件以及一些其他文件,无法修改。在此之前,我从未 seen/tried 嵌套 classes 并且在尝试查找有关该主题的相关信息时遇到了一些真正的困难。我发现的所有内容都在非模板 classes 上,尽管它应该非常相似,但 and/or 没有解释如何在单独的文件中实现它。据我所见,我的尝试方向是正确的,但无法编译。我试过了
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)
和一大堆其他我想不到的 a.t.m.. 如果有人能找出我的错误并向我解释,我将不胜感激。提前致谢!
我也包括 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;
^~~~~
以下是我根据uneven_mark
的建议得到的error/s
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
指的是StackLinked<DataType>
里面嵌套classStackNode
,也就是不是一个class模板,因此它不会获得任何模板参数列表 (<...>
)。
StackLinked<DataType>::StackNode::StackNode
指的是StackLinked<DataType>
里面StackNode
的构造函数。
其余的是与您在 class 定义中的声明相匹配的参数列表。
也就是说,如评论中所述,class 模板成员函数(以及它们嵌套的 classes 的成员函数)的定义通常需要放在头文件中。
编辑:添加了更多 code/additional 文件以及编译错误 所以我很难弄清楚如何为我的嵌套 class 实现构造函数。 这是我的 .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;
}
这是一项作业,提供了头文件以及一些其他文件,无法修改。在此之前,我从未 seen/tried 嵌套 classes 并且在尝试查找有关该主题的相关信息时遇到了一些真正的困难。我发现的所有内容都在非模板 classes 上,尽管它应该非常相似,但 and/or 没有解释如何在单独的文件中实现它。据我所见,我的尝试方向是正确的,但无法编译。我试过了
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)
和一大堆其他我想不到的 a.t.m.. 如果有人能找出我的错误并向我解释,我将不胜感激。提前致谢! 我也包括 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;
^~~~~
以下是我根据uneven_mark
的建议得到的error/ssleer@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
指的是StackLinked<DataType>
里面嵌套classStackNode
,也就是不是一个class模板,因此它不会获得任何模板参数列表 (<...>
)。
StackLinked<DataType>::StackNode::StackNode
指的是StackLinked<DataType>
里面StackNode
的构造函数。
其余的是与您在 class 定义中的声明相匹配的参数列表。
也就是说,如评论中所述,class 模板成员函数(以及它们嵌套的 classes 的成员函数)的定义通常需要放在头文件中。