Thread: Passing back objects from with functions. Datatype?

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Quote Originally Posted by Swerve
    I need something else.
    Yes, cars:
    Code:
    cars myfunction();
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 06: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, 01:08 PM
  5. Replies: 6
    Last Post: 05-06-2003, 03:08 PM