Thread: Passing object to function outside of loop

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Passing object to function outside of loop

    Is there a way to pass an object/pointer to object to a function outside of a loop?

    eg.

    Code:
    int main()
    {
        cout << "Welcome. Please choose your class:\n\n";
        cout << "K.\n\nKnight\nHP:100\nAttack:10\nDefence:10\n\n";
        cout << "N.\n\nNormal Person\nHP:100\nAttack:8\nDefence:12\n\n";
    
        while (true)
        {
            char selection = cin.get();
            cin.ignore();
            if ( selection == 'k' or selection == 'K')
            {
                cout << "Knight." << "\n";
                auto_ptr<Character> Hero(new Character("Knight",100,10,10,0,1));// set the values for the character
    
                battle(*Hero, *Enemy1);   // Passes the Character Hero to the battle function
                break;
            }
            if ( selection == 'n' or selection == 'N')
            {
                cout << "Normal Person." << "\n";
                auto_ptr<Character> Hero(new Character("Normal Person",100,8,12,0,1));
    
                battle(*Hero, *Enemy1);
                break;
            }
        }
        cout << "Exiting...";
        return 0;
    }
    I'm doing that right now, but what I'd like to do is this:

    Code:
    int main()
    {
        cout << "Welcome. Please choose your class:\n\n";
        cout << "K.\n\nKnight\nHP:100\nAttack:10\nDefence:10\n\n";
        cout << "N.\n\nNormal Person\nHP:100\nAttack:8\nDefence:12\n\n";
    
        while (true)
        {
            char selection = cin.get();
            cin.ignore();
            if ( selection == 'k' or selection == 'K')
            {
                cout << "Knight." << "\n";
                auto_ptr<Character> Hero(new Character("Knight",100,10,10,0,1));// set the values for the character
            }
            if ( selection == 'n' or selection == 'N')
            {
                cout << "Normal Person." << "\n";
                auto_ptr<Character> Hero(new Character("Normal Person",100,8,12,0,1));
            }
            battle(*Hero,*Enemy1);
            break;
        }
        cout << "Exiting...";
        return 0;
    }
    So that all I'd have to do is make the object, and call the battle function once total instead of once in every loop.

    Help is appreciated.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, don't create the variable inside the if statements (it will go out of scope).
    You could put the auto_ptr outside the ifs and then create the appropriate object inside the ifs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Is this way of doing it alright, or is there a better way?

    Code:
    while (true)
        {
            char selection = cin.get();
            cin.ignore();
            auto_ptr<Character> Hero(new Character("test",0,0,0,0,0));
            if ( selection == 'k' or selection == 'K')
            {
                cout << "Knight." << "\n";
                auto_ptr<Character> Knight (new Character("Knight",100,10,10,0,1));// set the values for the character
                Hero = Knight;
            }
            if ( selection == 'n' or selection == 'N')
            {
                cout << "Normal Person." << "\n";
                auto_ptr<Character> Normal(new Character("Normal Person",100,8,12,0,1));
                Hero = Normal;
            }
            battle(*Hero, *Enemy1);    // battle function

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, no, no.
    Code:
        while (true)
        {
            char selection = cin.get();
            cin.ignore();
            auto_ptr<Character> Hero; //(new Character("test",0,0,0,0,0));
            if ( selection == 'k' or selection == 'K')
            {
                cout << "Knight." << "\n";
                //auto_ptr<Character> Knight (new Character("Knight",100,10,10,0,1));// set the values for the character
                Hero = new Character("Knight",100,10,10,0,1); //Knight;
            }
            if ( selection == 'n' or selection == 'N')
            {
                cout << "Normal Person." << "\n";
                //auto_ptr<Character> Normal(new Character("Normal Person",100,8,12,0,1));
                Hero = new Character("Normal Person",100,8,12,0,1); //Normal;
            }
            battle(*Hero, *Enemy1);    // battle function
        }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Ok, I tried it:
    Code:
        while (true)
        {
            char selection = cin.get();
            cin.ignore();
            auto_ptr<Character> Hero;
            if ( selection == 'k' or selection == 'K')
            {
                cout << "Knight." << "\n";
                Hero = new Character("Knight",100,10,10,0,1); //Knight
            }
            if ( selection == 'n' or selection == 'N')
            {
                cout << "Normal Person." << "\n";
                Hero = new Character("Normal Person",100,8,12,0,1); //Normal
            }
            battle(*Hero, *Enemy1);    // battle function
        }
    But all I get is "error: no match for 'operator=' in 'Hero = (((Character*)operator new(24u)) ...' and so on.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Bah! Stupid auto_ptr.
    Try
    Code:
    Hero = auto_ptr<Character>( new Character("Normal Person",100,8,12,0,1) ); //Normal
    then.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Thanks, it works fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array of a class object into a function
    By maoqiu in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2007, 08:42 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. passing an object as an argument to a function (by reference)
    By odysseus.lost in forum C++ Programming
    Replies: 5
    Last Post: 12-15-2005, 12:06 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM