Thread: Calling a member function from a different file

  1. #1
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49

    Calling a member function from a different file

    Hi guys
    Im having some problem calling a member function in my main.cpp file. The class is in a header file and the member function itself is located in an addational cpp file. Im getting a linker error when I compile. I called it with classname::memberfunctio(). If anyone needs to see either of the cpp or the header file let me know. I didnt want to post I didnt need to because they are so big.

    Thanks
    Chris

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well you could try telling us what the linker error is and maybe provide the applicable code - like a shorter version of main that includes the member call.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Quote Originally Posted by cdonlan
    classname::memberfunctio()
    Maybe his problem is that he's leaving off the last letter of the function

    Some code would really help, so when you get the chance post it.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You must include the header that has the prototype for the function and/or the class definition that the function belongs to.

    CFoo.h
    Code:
    #ifndef FOO
    #define FOO
    
    class CFoo 
    {
      int m_iFoo; 
      public:
         CFoo(void):m_iFoo(0) {}
         
         void SetFooFunc(int iNewFoo)  {m_iFoo=iNewFoo;};
         
    };
    
    #endif
    MainLoop.cpp
    Code:
    #include "CFoo.h"
    
    CFoo TestFoo;
    
    
    int main(void)
    {
      Foo.SetFooFunc(5);
      return(0);
    }
    Project:
    • MainLoop.cpp



    Normally if you had a header file and a cpp file for CFoo you would add CFoo.cpp to the project. However in this case CFoo is completely defined as inline and so just including the header will suffice.

    This only relates to MSVC 6.
    Last edited by VirtualAce; 02-01-2005 at 12:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM