1

我是一个新手 C++ 编码器,显然不是很擅长。我在这个程序上遇到了很多麻烦。

  • 我的函数的左括号和右括号出现语法错误,<头 cpp 文件中的“”出现语法错误,以及缺少括号的错误。
  • 我的第一个堆栈无法识别(主驱动程序文件),并且在我的 StackType.cpp 文件中 -original是“未声明的标识符”。
  • 最后,左边Push必须有类/结构/联合 - 在我的for循环中,当用环填充第一个堆栈时。

我提前为所有这些问题道歉。您能给我的任何帮助将不胜感激!谢谢你。

======================堆栈头========================== ======

// File: StackType.h
// Stack template class definition.
// Dynamic array implementation

#ifndef StackType
#define StackType

template <class ItemType>
class StackType

{
private:
    int ItemType;
    ItemType *myStack;  // pointer to dynamic array
    int _top, _maxSize; // using underscores to remind that it's private

    public:

    StackType(int numRings = 50);       // Constructor
    StackType (const StackType<ItemType>&); // Copy Constructor


    // Member Functions
    void Push(ItemType);            // Push
    void Pop(ItemType &);           // Pop
    void stackTop(ItemType &) const;    // retrieve top
    bool stackIsEmpty() const;          // Test for Empty stack
    bool stackIsFull() const;       // Test for Full stack


    ~StackType();       // Destructor
};
#endif

=====================堆栈cpp文件========================== ========

#include "StackType.h"

#include "stdafx.h"
#include <iostream> 
#include <stdio.h>

// Constructor with argument, size is numRings, limit is 50 (set in .h header)
template <class ItemType>
StackType<ItemType>::StackType()
{  
    _maxSize = numRings; 
    _top = -1; 
}

// Copy Constructor
template <class ItemType>
StackType<ItemType>::StackType(const StackType<ItemType>& original :  
                                       _maxSize(original._maxSize), top(original._top)
{
    myStack = new ItemType[_maxSize];
    for (int i = 0; i <= top; i++) myStack[i] = original.myStack[i];
}


// Destructor
template <class ItemType>
StackType<ItemType>::~StackType()
{ 
    delete [] myStack;
}

// Push
template <class ItemType>
void StackType<ItemType>::Push(StackType<ItemType> ringVal)
{
    if(stackIsFull()) cout << "\t There is not enough available memory = the stack is 
                                  full!" << endl;
    else myStack[++_top] = ringVal;
}

// Pop
template <class ItemType>
void StackType<ItemType>::Pop(StackType<ItemType> &ringVal)
{
    if(stackIsEmpty()) cout << "\t The stack is empty!" << endl;
    else ringVal = myStack[_top--];
}

// Retrieve stack top without removing it
template <class ItemType>
void StackType<ItemType>::stackTop(StackType<ItemType> &ringVal) const
{
    if(stackIsEmpty()) cout << "The stack is empty!";
    else ringVal = myStack[_top];
}

// Test for Empty stack
template <class ItemType>
bool StackType<ItemType>::stackIsEmpty() const
{ 
    return (_top < 0); 
}

// Test for Full stack
template <class ItemType>
bool StackType<class ItemType>::stackIsFull() const
{ 
    return (_top >= (_maxSize - 1)); 
}
 // end StackType.cpp

==========================主驱动文件====================== ==================

#include "StackType.h"
#ifdef _DEBUG
#include "StackType.cpp"
#endif // _DEBUG


#include <stack>
#include "StdAfx.h"
#include <iostream>
using namespace std;

//  Global Variable - Counter to display the number of moves.
int count = 0; 

class StackType;

//  Functions Prototypes
void MoveRings(StackType<ItemType>&, StackType<ItemType>&);
//  Function to move the rings
void Pegs(int D,StackType<ItemType>& b,StackType<ItemType>& e, StackType<ItemType>& h);
//  This is a recursive function.  
void Display (int, StackType <ItemType>& , StackType<ItemType>&, StackType<ItemType>&); 
//  Function to display the pegs  

//  Main - Driver File
int main()
{

    // create 3 empty stacks
    StackType<ItemType> FirstPeg;   // Receiving an error that this is not identified
    StackType<ItemType> EndPeg;
    StackType<ItemType> HelperPeg;

    // Number of rings.
    int numRings;

    cout << "\n\t *********** Rings to Pegs (Towers of Hanoi) ***********\n" << endl;
    cout << "\t Please Enter the number of rings you want to play with: ";
    //  Input number of rings
    cin >> numRings;    
    cout << endl;
    while(numRings < 0 || isalpha(numRings))  //  To make sure that the user did not 
                                              //  enter an invalid number
    {
        cout << "  Your entry is invalid. Please use only integers. Please re-
                                                                              enter: ";
        cin >> numRings;
        cout << endl;
    }

    for(int i = 1; i <= numRings; i++)
     // Fill the first peg with the number of rings.
    {
        FirstPeg.Push(i);
    }


    Pegs(int, StackType<ItemType>&, StackType<ItemType>&, StackType<ItemType>&); 
    //  To call the recursive function that will move the rings
    Display (int, StackType<ItemType>&, StackType<ItemType>&, StackType<ItemType>&); 
    //  To call the display function

    cin.clear();
    cin.ignore('\n');
    cin.get();
    return 0;

}

//  This function will move an ring from first peg to the second peg
void MoveRings(StackType<ItemType>& beg, StackType<ItemType>& theEnd) //End
{
    int r; // disk will be removed from one stack and added to the other

    beg.Pop(r);//pop from source

    theEnd.Push(r);//and move to target

}

//  This function displays the moves
void Display(int R, StackType<ItemType>& toBegin , StackType<ItemType>& toEnd,  
             StackType<ItemType>& toHelp) 

{
    StackType<int> B;// create temporarily first stack 
    StackType<int> E;// create temporarily End(End) stack 
    StackType<int> H;// create temporarily helper stack 
    for(int i = 1; i <= R; i++)
    {
        toBegin.Pop(i);//moves the ring from source
        B.Push(i);//to the temporarily stack to display it
        cout << "Beginning Peg:" << &B << endl;

        toEnd.Pop(i);//moves the ring from source
        E.Push(i);//to the temporarily stack to display it
        cout << "  End(Final) Peg: " << &E << endl;

        toHelp.Pop(i);//moves the ring from source
        H.Push(i);//to the temporarily stack to display it
        cout << "  Helper Peg:" << &H << endl;
    }
}



//-------------------------------------------------------------------

void Pegs(int D,StackType<ItemType>& b,StackType<ItemType>& e,StackType<ItemType>& h) 
//  This is a recursive function. 
{
    if (D == 0) // The base 
    {
        return 1;
    }

    else if(D == 1)              //  If there is only one ring, move this ring from the 
                                 //  first peg to the end(final) peg
    {
        MoveRings(b, e); // moves the ring from the first to the end(final) peg 
        cout<<"  Really? You have entered one ring..." << endl;
        cout<<"  It moves directly from the first peg to the End peg." << endl;

        count++; // increment the number of moves

        cout << "There has been " << count << " move. "<< endl;// display the          
                                                                   // number of moves
        Display (D, b, e, h);

    }
    else if (D > 1) // a recursive function in order to move the rings
    {

            Pegs(D - 1, b, e, h);    //  to move N-1 rings from the first peg to the
                                     //  end(final) peg by using the helper peg

        MoveRings(b, e);//  to move the last ring to the end(final) peg 
        count++; //  increment the number of steps before displaying
        cout << "There has been " << count << " moves. "<< endl;

Pegs(D - 1, b, e, h); 
    //  to move N-1 rings from the helper peg to the end(final) peg with the help of                    
    //  first peg

    //Display ( D(rings), First Peg, End(Final) Peg, Helper Peg );
    }
}
4

1 回答 1

2

我可以立即看到的一个问题是您的头文件定义StackType为防止双重包含,这也用作类名。之后#define StackType,它最终成为一个扩展为空的宏,因此您的代码看起来像class { ... }.

您应该使用一个符号来防止不用于其他任何内容的双重包含。对于名为 StackType.h 的文件,典型使用的是 STACKTYPE_H。

解决此问题后,您遇到的其他一些问题可能会消失。如果您遇到更多问题,请返回更新,如果有,请发布确切的编译器错误。

于 2012-11-25T23:39:21.357 回答