Stack Question With Templates

This is a discussion on Stack Question With Templates within the C++ Programming forums, part of the General Programming Boards category; I have a stack.cpp defined as the below. My problem is I also have 2 separate classes infix.h and postfix.h ...

  1. #1
    Anonymous Freak
    Guest

    Stack Question With Templates

    I have a stack.cpp defined as the below. My problem is I also have 2 separate classes infix.h and postfix.h and I want to have the stack accessible from both of them and not sure exactly where to implement them. I'm almost 99% sure it has to have to do something with where the stack objects are defined

    If I put

    Infix InEx;
    Postfix PostEx;

    TStack<double> Numbers;
    TStack<char> Op;

    in main I can't manipulate the stack in
    the class. I need to somehow manipulate the Postfix object by passing it in with a convert function like the below.

    Infix.Convert(PostEx);


    template <class TItem> class TStack
    {
    private:
    int Top;
    TItem Data[64];
    public:
    TStack ();
    void Push (TItem Item);
    void Pop (TItem &Item);
    };

    template <class TItem>
    inline TStack<TItem>::TStack ()
    {
    Top = -1;
    }

    template <class TItem>
    inline void TStack<TItem>::Push (TItem Item)
    {
    if (Top < 64 - 1 )
    {
    Top++;
    Data[Top] = Item;
    }
    }

    template <class TItem>
    inline void TStack<TItem>::Pop (TItem &Item)
    {
    if (Top != -1)
    Top--;

    }

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    next time use code tags.

  3. #3
    Anonymous Freak
    Guest

    How?

    How do we use code tags?

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    *sigh*
    It's only the first post on every single forum. << !! Posting Code? Read this First !! >>

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Put the whole stack in a header file with definitions and everything. Then #include the header file in every file that needs the stack.
    There's no need for an unnamed namespace, because templates are special. Currently the 'export' keyword is unsupported, so putting the definitions in .cpp files won't work.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Anonymous Freak
    Guest

    That's Exactly What I Did But

    I have everything already in the stack.h file but can't seem to figure out how I can have acess in 2 different classes.The stack files are included everywhere needed

    I want to be able to manipulate these 2 stacks below:

    TStack<double> Numbers;
    TStack<char> Op;

    In better example I need to use Numbers and Op Stack in one definition in multiple classes.

    So I can have the same 2 stacks above accessed in 2 different classes.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: That's Exactly What I Did But

    Originally posted by Anonymous Freak

    In better example I need to use Numbers and Op Stack in one definition in multiple classes.
    Use global variables.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-04-2007, 12:01 AM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  3. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 08:32 PM
  4. member templates question
    By what3v3r in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2006, 01:07 AM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 10:50 AM

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