2012-11-25 55 views
1

我是新手C++編碼器,顯然不是很擅長。我對這個程序有很大的麻煩。堆棧遞歸程序問題

  • 我對我的開放獲取語法錯誤,在我的頭cpp文件對我的「<」我的功能,語法錯誤結束括號,然後我就是缺少括號錯誤。
  • 我的第一個堆棧不被識別(主驅動程序文件),在我的StackType.cpp文件中 - original是一個「未聲明的標識符」。
  • 最後,Push的左邊必須有class/struct/union - 在我的for循環中填充第一個堆棧的環時。

我對所有這些問題提前表示歉意。任何幫助你可以給我將不勝感激! 謝謝。

====================== Stack Header ====================== ==========

// 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); 
    } 
} 

回答

2

一個問題是我可以立即看到您的頭文件定義了StackType以防止雙重包含,它也被用作類名。在#define StackType之後,它最終變成了一個擴展爲無的宏,因此您的代碼看起來像class { ... }

您應該使用符號來防止沒有用於其他任何內容的雙重包含。對於名爲StackType.h的文件,通常使用的是STACKTYPE_H。

修復此問題後,您遇到的其他一些問題可能會消失。如果遇到更多問題,請返回更新,並在發佈確切的編譯器錯誤後再發布。

+0

謝謝!我這樣做,並最終讓它工作 - 它花了一段時間。不幸的是,它沒有刪除語法錯誤。我是否缺少任何預處理器指令? – user1851879

+0

現在第一個問題已解決,請使用代碼的當前版本和從編譯器獲得的消息更新您的問題。 –

+0

我仍然在我的函數上打開和關閉括號時出現語法錯誤,我的主驅動程序文件中的「<」語法錯誤,以及我是人物的錯誤。 – user1851879