Accessing a struct element in a different class...

This is a discussion on Accessing a struct element in a different class... within the C++ Programming forums, part of the General Programming Boards category; This is my header file for my Stack class... Code: // Stack.h #ifndef STACK_H #define STACK_H #include "Utility.h" const int ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    Accessing a struct element in a different class...

    This is my header file for my Stack class...

    Code:
    // Stack.h
    
    #ifndef STACK_H
    #define STACK_H
    #include "Utility.h"
    
    const int MAXSTACK = 10;
    
    struct Stack_entry
    {	char element;
    	bool flag;
    };
    
    class Stack {
    public:
    	Stack();
    	bool empty() const;
    	Error_code pop();
    	Error_code top(Stack_entry &item) const;
    	Error_code push(const Stack_entry &item);
    
    private:
    	int count;
    	Stack_entry entry[MAXSTACK];
    };
    #endif
    How can I access
    Code:
    struct Stack_entry
    {	char element;
    	bool flag;
    };
    from main once I've initialized a stack from this class?

    [edit] - Nevermind figured it out...

    Solution for any interested:
    I declared a variable of type Struct_entry in my main like so:

    Code:
    Struct_entry x;
    then used my top function to put the top of my stack into x:

    Code:
    someStack.top(x);
    then accessed it normally like:

    Code:
    x.flag
    
    or
    
    x.element
    Just remember to pop off the old value on the stack and push the new value once you're done with it
    Last edited by Sparrowhawk; 03-09-2009 at 05:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 05:05 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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