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

すぐにわかる問題の 1 つは、ヘッダー ファイルStackTypeがクラス名としても使用される二重インクルードを防止するように定義していることです。の後#define StackType、何も展開されないマクロになるため、コードは のようになりますclass { ... }

他に使用されていない二重包含を防ぐために、シンボルを使用する必要があります。使用する典型的なものは、StackType.h と呼ばれるファイルの STACKTYPE_H です。

これを修正すると、発生している他の問題が解消される可能性があります。さらに問題が発生している場合は、更新して戻ってきてください。もしそうなら、正確なコンパイラ エラーを投稿してください。

于 2012-11-25T23:39:21.357 に答える