谁能帮我解决以下编译错误 - 关于数组的两个实现
Can anyone help me with the following compilation errors - regarding two implementations of an Array?
错误:
分隔符.cpp(41(: 错误 C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::运算符 + (_String_iterator<_Elem,_Traits,_Alloc>::d ifference_type,std::_String_iterator<_Elem,_Traits,_Alloc>(' :无法从 'char' 推断出 'std::_String_iterator<_Elem,_Traits,_Alloc>' 的模板参数1> C:\Program Files (x86(\Microsoft Visual Studio 10.0\VC\include\xstring(434(:请参阅"std::operator +"的声明
stacklinked.cpp(20): error C2061: syntax error : identifier 'StackNode'
stacklinked.cpp(28): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
stacklinked.cpp(28): error C2063: 'StackNode' : not a function
stacklinked.cpp(28): fatal error C1903: unable to recover from previous error(s); stopping compilation
响应式代码:
#ifndef STACKLINKED_CPP
#define STACKLINKED_CPP
#include <iostream>
#include "StackLinked.h"
//--------------------------------------------------------------------
template <typename DataType>
StackLinked<DataType>::StackNode(const DataType& newDataItem,
StackLinked<DataType>::StackNode* nextPtr)
//::StackNode
// Creates a stack node containing item newDataItem and next pointer
// nextPtr.
: dataItem(newDataItem), next(nextPtr)
{
}
//--------------------------------------------------------------------
template <typename DataType>
StackLinked<DataType>::StackLinked(int maxNumber)
: top(0)
// Creates an empty stack. The parameter maxNumber is provided for
// compatability with the array implementation and is ignored.
{
}
//--------------------------------------------------------------------
template <typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
// Copy constructor for linked stack
: top( 0 )
{
(void) operator=(other); // Use operator=, ignore return value
/*
// Alternatively, could duplicate essentially all the code from
// operator= and insert below.
if( ! other.isEmpty() ) {
// Copy first node
top = new StackNode(other.top->dataItem, 0);
StackNode *otherTemp = other.top->next;
StackNode *thisTemp=0, *thisPrevious=top;
// Copy rest of nodes
while( otherTemp != 0 )
{
thisTemp = new StackNode(otherTemp->dataItem, 0);
thisPrevious->next = thisTemp;
thisPrevious = thisTemp;
otherTemp = otherTemp->next;
}
}
*/
}
//--------------------------------------------------------------------
template <typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
// Overloaded assignment operator for the StackLinked class.
// Because this function returns a StackLinked object reference,
// it allows chained assignment (e.g., stack1 = stack2 = stack3).
{
// Self-assignment protection
if( this != &other ) return *this;
clear(); // Clear existing nodes
if( ! other.isEmpty() )
{
// Copy first node
top = new StackNode(other.top->dataItem, 0);
StackNode *otherTemp = other.top->next;
StackNode *thisTemp=0, *thisPrevious=top;
// Copy rest of nodes
while( otherTemp != 0 )
{
thisTemp = new StackNode(otherTemp->dataItem, 0);
thisPrevious->next = thisTemp;
thisPrevious = thisTemp;
otherTemp = otherTemp->next;
}
}
return *this;
}
//--------------------------------------------------------------------
template <typename DataType>
StackLinked<DataType>::~StackLinked()
// Destructor. Frees the memory used by a stack.
{
clear();
}
//--------------------------------------------------------------------
template <typename DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw (logic_error)
// Inserts newDataItem onto the top of a stack.
{
if (isFull()) {
// Not likely with linked implementation
throw logic_error("push() while stack full");
}
top = new StackNode(newDataItem, top);
}
//--------------------------------------------------------------------
template <typename DataType>
DataType StackLinked<DataType>::pop() throw (logic_error)
// Removes the topmost item from a stack and returns it.
{
if (isEmpty()) {
throw logic_error("pop() while stack empty");
}
StackNode* temp = top;
top = top->next;
DataType value = temp->dataItem;
delete temp;
return value;
}
//--------------------------------------------------------------------
template <typename DataType>
void StackLinked<DataType>::clear()
// Removes all the data items from a stack.
{
for (StackNode* temp = top; top != 0; temp = top)
{
top = top->next;
delete temp;
}
// Invariant: At this point in the code, top == 0.
// Top does not heed to explicitly set to 0. It was
// either 0 before the loop, or emerged from the loop as 0.
}
//--------------------------------------------------------------------
template <typename DataType>
bool StackLinked<DataType>::isEmpty() const
// Returns true if a stack is empty. Otherwise, returns false.
{
return top == 0;
}
//--------------------------------------------------------------------
template <typename DataType>
bool StackLinked<DataType>::isFull() const
// Returns true if a stack is full. Otherwise, returns false.
{
return false;
/*
// Alternatively, can use implementation below.
// This is a somewhat awkward way to test if the list is full.
// If a node can be successfully allocated than the list is not
// full. If the allocation fails it is implied that there is no
// more free memory therefore the list is full.
// We are not aware of any other standard/portable way of
// performing the test. And this can fail due to external issues
// such as the system exhausting swap or another thread stealing
// the remaining memory between when isFull returns its result and
// the caller does something that assumes that isFull() returned
// a valid answer.
//
// Alternatives include just the line "return false", which is
// probably good enough in this context, or platform-dependent
// checks for available memory.
StackNode* temp;
DataType junk;
try
{
temp = new StackNode( junk, 0 );
}
catch ( bad_alloc &e )
{
return true;
}
delete temp;
return false;
*/
}
//--------------------------------------------------------------------
template <typename DataType>
void StackLinked<DataType>::showStructure() const
// Linked list implementation. Outputs the data elements in a stack.
// If the stack is empty, outputs "Empty stack". This operation is
// intended for testing and debugging purposes only.
{
if( isEmpty() )
{
cout << "Empty stack" << endl;
}
else
{
cout << "Topt";
for (StackNode* temp = top; temp != 0; temp = temp->next) {
if( temp == top )
{
cout << '[' << temp->dataItem << "]t";
}
else
{
cout << temp->dataItem << "t";
}
}
cout << "Bottom" << endl;
}
}
#endif //#ifndef STACKLINKED_CPP
这是头文件,StackLinked.h
//--------------------------------------------------------------------
//
// Laboratory 6 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
这是另一个头文件,Stack.h
//--------------------------------------------------------------------
//
// Laboratory 6 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
因此,我已经包含了所有相应的标头,因为该程序利用了继承。 请继续帮助我调试此程序。 所有的帮助都非常感谢。
您可能想要替换
StackLinked<DataType>::StackNode(...)
由
StackLinked<DataType>::StackNode::StackNode(...)
那是类StackNode
的构造函数,通常StackNode::StackNode
在类StackLinked<DataType>
中调用。
相关文章:
- 在给定程序中降低矢量数组实现堆栈的时间复杂度有哪些不同的可能方法?
- C++ 使用数组实现堆栈
- 使用 C++ STL 的数组实现
- 为自定义数组实现迭代器
- 二进制搜索树的数组实现
- Boyer Moore 动态数组实现
- 为什么这个普通的数组实现比STD ::向量实现性能慢
- 通过访问二维数组实现双线性插值的概念类似于一维数组
- 通过数组实现BST,通过链表实现堆
- 可以使用 1D 数组实现 0-1 背包吗?
- 使用循环数组实现矢量
- 使用链表数组实现哈希表
- 队列的数组实现:奇怪的输出
- 如何使用指针数组实现节点
- 使用数组实现一个简单的队列
- 多维数组实现
- 堆栈的数组实现
- IntSet数组实现
- 用字符数组实现BigInteger的上限是多少?
- 链表vs.动态数组实现一副牌?c++