calling a class method within different class method

This is a discussion on calling a class method within different class method within the C++ Programming forums, part of the General Programming Boards category; Hi, How do you call a class method inside a different class method? Here's what I have: Code: void cMenu::mission1() ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Question calling a class method within different class method

    Hi,

    How do you call a class method inside a different class method? Here's what I have:

    Code:
    void cMenu::mission1()
    {
    	cInventory.printInventory();
    }
    Code:
    void cInventory::printInventory()
    {
           mItems = mBlade + mRope + mStick + mPole + mString + mKnife + mStars +
    		mBo + mSword + mBowArrow;
    	cout << "Total Items: " << mItems << endl << endl;
    
    	cout << "Loose inventory: "  << endl;
    	cout << "---------------- "  << endl;
    	cout << "Blade:  " << mBlade  << endl;
    	cout << "Rope:   " << mRope   << endl;
    	cout << "Stick:  " << mStick  << endl;
    	cout << "Pole:   " << mPole   << endl;
    	cout << "String: " << mString << endl;
    	
    	cout << endl;
    	cout << "Finished Weapons: " << endl;
    	cout << "----------------- " << endl;
    	cout << "Knife:         " << mKnife    << endl;
    	cout << "Stars:         " << mStars    << endl;
    	cout << "Bo:            " << mBo       << endl;
    	cout << "Sword:         " << mSword    << endl;
    	cout << "Bow and Arrow: " << mBowArrow << endl << endl;
    }
    Here's my error. I think it's because I have declared the variables in cInventory private but I am not sure if that's what it is. I have included cInventory's header file in cMenu.

    Code:
    1>g:\exercises\village warrior\cmenu.cpp(59) : error C2143: syntax error : missing ';' before '.'
    Thanks so much.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You call a function by name. In this case, the name is "printInventory". (Notice that if you're already in a member function, you don't need to somehow specify which object you want to call the function with; it will automatically use the object you're currently a member of.)

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Question

    Hi tabstop,

    I am calling it though from a completely different class. Is that what you meant?

    Thanks!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by alyeska View Post
    Hi tabstop,

    I am calling it though from a completely different class. Is that what you meant?

    Thanks!
    Sorry, I read that but it didn't register.

    If you want to call a member function, you must have an object of type cInventory to call it with. (Unless it's a static function. Then you would need the scope resolution with cInventory:: -- but all the member variables would have to be static too, et cetera.)

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    So I now see what you meant.

    Thanks, tabstop. I got it working now..

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Then you would need the scope resolution with cInventory:: -- but all the member variables would have to be static too, et cetera.)
    For me this is always an indication that I haven't well-designed my class structure or that your function takes the wrong arguments (or that you should move functionalities from one class to the other).

    Should you call a member function (operating on member data) from outside the class, probably you can pass an object of that class to the function? Then it will look like this:

    Code:
    void cMenu::mission1(cInventory const& inv) const // if argument is to be constant
    {
    	inv.printInventory();
    }
    edit: if this is the functionality of "cMenu::mission1" , then you might want to consider making it a free function. Minimalising class functionality is always a good choice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inline templated class method as standalone function in namespace
    By monikersupreme in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2008, 12:38 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. calling OpenGL methods from outside the main class
    By Hector in forum Game Programming
    Replies: 2
    Last Post: 06-22-2006, 08:23 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM

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