Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I get a compile error :inlab12a.h:29: error: \'int Stack::top\' is private main1

ID: 3546470 • Letter: I

Question

I get a compile

error :inlab12a.h:29: error: 'int Stack::top' is private
main12.cc:41: error: within this context
main12.cc:41: error: 'charStack.Stack::top' cannot be used as a function

and I cannot get around it. Can you please help me?



// File:       main12.cc
//
// This program demonstrates how a stack works.  It uses a stack to reverse
// a string of characters.  The characters are pushed onto the stack in the
// order they are read, and then printed in the order they are popped back
// off.  A stack is not needed for such a simple problem, but the simplicity
// allows the student to focus on the stack.
//
// The main program prompts the user for a string, and then reads one char
// at a time and pushes it onto the stack.  When this is finished, the
// characters are printed in the order that they are popped off of the stack.
// Stack elements are characters

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

int main()
{
    Stack charStack;   //a stack which will hold characters
    char ch;           //a single input character
    
    // Tell cin not to skip whitespace.
    cin.unsetf(ios::skipws);  
    
    // Get a string from the user and push it onto the stack one
    // char at a time.
    cout << "Enter a line of text." << endl;        
    while(cin>>ch && ch!=' ')
        charStack.push(ch);

    //Finish the algorithm by adding code to pop a
    //character at a time and print them in the order they
    //are popped.

****************************************

//this is the code that I need to make

//work and compile correctly

****************************************   
    while (!charStack.empty())
        
        {
        charStack.pop();
        cout << charStack.top() << endl;
        }
        
****************************************
    
    


    cout << endl;
    
    return 0;
}

inlab12a.h:

http://pastebin.com/qykE9GaK


inlab12a.cc:

http://pastebin.com/XKvnnWgv

Explanation / Answer

/*
top is private member of class and u cant acces that directly.
update code as highlighted below.
*/


//
//This file contains the Stack class definition. The stack
//is to be implemented using an array to contain the stack
//elements.

#ifndef STACK_CLASS_H
#define STACK_CLASS_H

//used to indicate the type of data held by the stack
typedef char DataType;

//the most number of elements held by the stack
const int MAX_STACK_SIZE = 100;

struct StackNode
{
    DataType data;
    StackNode *next;
};

typedef StackNode* NodePtr;

class Stack
{
    private:
        //private data members, stack elements and top
        DataType elements[MAX_STACK_SIZE];
        int top;
    public:
        //constructor which initializes the top
        Stack();
       
        //stack modification operations
        void push (DataType item);
        DataType pop();
        int getTop() { return top; }
        //stack test methods
        bool empty();
        bool full();
};
#endif

/ main function

#include "inlab12a.h"
#include <iostream>
using namespace std;
int main()
{
Stack charStack; //a stack which will hold characters
char ch; //a single input character
// Tell cin not to skip whitespace.
cin.unsetf(ios::skipws);
// Get a string from the user and push it onto the stack one
// char at a time.
cout << "Enter a line of text." << endl;
while(cin>>ch && ch!=' ')
charStack.push(ch);
//Finish the algorithm by adding code to pop a
//character at a time and print them in the order they
//are popped.
//this is the code that I need to make
//work and compile correctly
while (!charStack.empty())
{
charStack.pop();
cout << charStack.getTop() << endl;
}
cout << endl;
system("pause");
return 0;
}