Passing back objects from with functions. Datatype?

This is a discussion on Passing back objects from with functions. Datatype? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am declaring an object from within a function, and am trying to pass the object back into main. ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Passing back objects from with functions. Datatype?

    Hi,

    I am declaring an object from within a function, and am trying to pass the object back into main.

    When looking up what datatype to use to hold the object,(where you could use 'void' in a function declaration), all I find are the fundamental datatypes such as int and bool.

    Hear is the part of the function:

    Code:
    switch (choice)
    {
    case 1:
        {
            cout << "Before creating an object of type car" << endl;
    
                cars temp;    //construct an object called 'temp' of type 'cars'
    
                cout << "Enter the MPG" << endl;
                cin >> temp.MPG;
    
                cout << "After creating an object of type car" << endl;
    
                return temp;
           
        }
    }

    I want to return the object 'temp' of class 'cars' back to main, but I'm finding the fundamental datatypes don't work.

    Thanks for any help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by Swerve
    I want to return the object 'temp' of class 'cars' back to main, but I'm finding the fundamental datatypes don't work.
    Perhaps because you did not declare the function as returning a cars?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks laserlight!

    Yes, this is the problem.

    If I prototype:

    int myfunction();

    then use

    return temp;

    'temp' will be trying to be held in a int return type.

    Since temp is an object, I cannot use an int, right?

    I need something else. I looked at using a typedef, but that says it would still be an int, just with a different name.

    How do use something instead of void, int, bool etc?
    Last edited by Swerve; 03-20-2009 at 11:51 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by Swerve
    I need something else.
    Yes, cars:
    Code:
    cars myfunction();
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  2. Passing functions between files
    By Duskan in forum C Programming
    Replies: 9
    Last Post: 04-17-2007, 07:44 AM
  3. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 05:48 PM
  4. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 12:08 PM
  5. Replies: 6
    Last Post: 05-06-2003, 03:08 PM

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