Problem with class and stacks

This is a discussion on Problem with class and stacks within the C++ Programming forums, part of the General Programming Boards category; In the following program how do i output 'maxelem' and 'top' the cout line is giving me an undefined error.. ...

  1. #1
    Unregistered
    Guest

    Unhappy Problem with class and stacks

    In the following program how do i output 'maxelem' and 'top' the cout line is giving me an undefined error..

    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>

    class IntStack
    {
    public:
    IntStack(int num) { top = 0; maxelem = num; s = new int[maxelem]; }
    void push(int t)
    {
    if (top == maxelem) return;
    s[top++] = t;
    }
    int pop()
    {
    if (top == 0) return -1;
    return s[--top];
    }
    void display()
    {
    if (top == 0) { cout << "(empty)\n"; return; }
    for (int t=0 ; t < top ; t++) cout << s[t] << " ";
    cout << "\n";
    }
    int empty() { return top == 0; }
    private:
    int *s;
    int top;
    int maxelem;
    };

    void main()
    {
    IntStack *s = new IntStack(100);
    int d;

    s->push(0);
    s->push(10);
    s->push(1);
    s->push(5);
    s->push(4);
    s->push(6);
    s->push(5);
    s->push(9);
    s->display();
    s->push(6);
    s->push(99);
    s->push(8);
    s->push(55);
    s->pop();
    cout <<"Depth of stack: " <<maxelem<< ", Max Depth: "<<top;
    int wait;
    cin >> wait;
    }

  2. #2
    Ted
    Ted is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    top and maxelem are private data members of class IntStack.
    You need a public member function that reurns the value of top and the value of maxelem,. e.g., from main: s->getTop() and s->getMaxelem().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 02:05 AM
  2. Dynamic storage class?
    By Xinok in forum C++ Programming
    Replies: 12
    Last Post: 12-20-2005, 12:14 PM
  3. Problem with deck class.
    By SlyMaelstrom in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2005, 05:00 PM
  4. Stacks and enum questions
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 04-25-2005, 06:39 PM
  5. Dumping singly linked list into 2 stacks.
    By strotee76 in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2004, 05:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21