returning a stack

This is a discussion on returning a stack within the C++ Programming forums, part of the General Programming Boards category; Is it possible to return a stack to main? If so what is the proper way? I have a simple ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    returning a stack

    Is it possible to return a stack to main? If so what is the proper way?
    I have a simple function add that adds numbers to a stack, then I have a
    Code:
    return mystack;
    and in main I have:
    Code:
    stack <double> x;
    x = add();
    cout << "stack has: " << x << endl;
    but this is not working

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    What does "not working" mean? Compile errors? Runtime errors?

    What is the definition of "add"?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    compile errors,
    I am getting an error
    19 D:\Documents and Settings\Suchy\Desktop\stack_practice.cpp no match for 'operator=' in 'x = add()'
    at the line:
    x = add();

    its just:
    double add ()
    and I believe that is the problem since the function is declared as an double, but how can I declare it as a stack of double?
    Last edited by Suchy; 04-30-2007 at 10:35 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if add returns a single double you cant assign it to a stack. have the add function return a stack<double>

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you want it to return a stack of doubles, then tell it so! double is a datatype, just like 'stack<double>'.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    yeah I want to return the whole stack of doubles

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i know. what i was trying to say is that you just have to change the return type. 'double' is a datatype.. 'stack<double>' is another datatype.
    Code:
    stack<myDataType> myFunc()
    {
       // ...
    }
    hope it helps!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

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