1

I am a novice C++ coder and obviously not very good at it. I am having an immense amount of trouble with this program.

  • I am getting syntax errors on my opening and closing parenthesis on my functions, syntax errors on my "<" in my header cpp file, and errors that I'm missing parenthesis.
  • My first stack is not recognized (main driver file) and in my StackType.cpp file - original is an "undeclared identifier".
  • Lastly, the left of Push must have class/struct/union - in my for loop when filling the first stack with the rings.

I apologize for all of these issues in advance. Any help you could give me would be greatly appreciated! Thank you.

======================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

=====================Stack cpp file==================================

#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

=========================Main Driver file=======================================

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

1 Answer 1

2

One problem that I can see immediately is that your header file defines StackType to prevent double inclusion, which is also used as a class name. After #define StackType, it ends up being a macro that expands to nothing, so your code looks like class { ... }.

You should use a symbol to prevent double inclusion that isn't used for anything else. The typical thing to use is STACKTYPE_H for a file called StackType.h.

Once you've fixed this, some other problems you're experiencing might go away. Please come back with an update if you're having more problems, and post the exact compiler errors if you do.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! I did that and finally got it to work - it took a while. Unfortunately, it didn't remove the syntax errors. Am I missing any of the preprocessor directives?
Now that the first issue is fixed, please update your question with the current version of the code and the messages you're getting from the compiler.
I am still getting syntax errors on my opening and closing parenthesis on my functions, syntax errors on my "<" in my main driver file, and then the errors that I'm the characters as well.
I am still getting syntax errors on my opening and closing parenthesis on my functions, syntax errors on my "<" in my main driver file, and then the errors that I'm missing the characters as well. Also, my first peg is still not showing up.I definitely got some new errors as well. Such as: StackType functions are note members of Global Namespace; Syntax error: identifier StackType
It's difficult to help you without seeing the exact messages. I need to see what messages appear and what lines they are about.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.