gpt4 book ai didi

c++ - 谁能帮我解决以下编译错误——关于数组的两个实现?

转载 作者:行者123 更新时间:2023-11-28 06:56:30 24 4
gpt4 key购买 nike

错误:
delimiters.cpp(41): error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::operator + (_String_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_iterator<_Elem,_Traits,_Alloc>)':无法推断'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 << "Top\t";
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

因此,由于该程序使用继承,因此我已经包含了所有相应的 header 。请继续帮我调试这个程序。非常感谢所有帮助。

最佳答案

你可能想替换

StackLinked<DataType>::StackNode(...)

StackLinked<DataType>::StackNode::StackNode(...)

这是类 StackNode 的构造函数照常打电话StackNode::StackNode类内StackLinked<DataType> .

关于c++ - 谁能帮我解决以下编译错误——关于数组的两个实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23108981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com