Object Arrays to Functions

This is a discussion on Object Arrays to Functions within the C++ Programming forums, part of the General Programming Boards category; I hate to be a bug, but i'm having a hell of a time figure out how to pass an ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    4

    Post Object Arrays to Functions

    I hate to be a bug, but i'm having a hell of a time figure out how to pass an array of OBJECTS to a function.

    _________________________________________________
    //FUNCTION DECLARATION
    void Menu(stock);
    void GetInfo(stock );

    //ALL DONE IN MAIN..NOT CLASS

    //ARRAY
    stock s[2];
    s[0] = stock("Toys R Us",50,2.99);
    s[1] = stock("iCafe Canada",100, 6.99);

    //FUNCTION CALL

    Menu(*s);


    //FUNCTION
    void Menu(stock *s)
    {
    GetInfo(s);
    }

    void GetInfo( *s)
    __________________________________________________ _

    Now, if i remove the GetInfo Function, the program compiles fine, but then when i go to RUN i get a linking error.

    unresolved external symbol "void __cdecl Menu(class stock)"

    I dont get it, i don't know how to make a pointer out of this array of OBJECTS, because of the CONSTRUCTORS.

    Any help would be greatly appretated.

    Ulysses

  2. #2
    Unregistered
    Guest
    change

    void Menu(stock);
    void GetInfo(stock);

    to

    void Menu(stock *);
    void GetInfo(stock *);

    and the call would be

    Menu(s);

    and the definition would be

    void Menu(stock * s)

    and in the definition go

    GetInfo(s);

    The name s IS a pointer, or more precisely is handled by the compiler as a pointer, to the first element of s.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The constructor for each object is called the moment you created the array!

    Try this instead:

    stock*ptr[ num_objects ]; //...an array of pointers to objects...

    ptr[0] = &stock("Toys R Us",50,2.99);

    ///...etc...


    For the function, it will be a stock** parameter.

    Hope that works for you...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    Thank you again,

    Sorry for being a bug, my only hope is that someone else may have this problem and learn from your HELP.

    Thank you again,

    It works great now=)

    Cheers
    Ulysses A. Freeman

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I didnt help on this one but thats why people hang out at forums like this, its educational for everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 04:29 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  3. Object Arrays and Inheritance
    By AceHigh in forum C++ Programming
    Replies: 7
    Last Post: 07-28-2002, 05:08 AM
  4. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 09:57 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM

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