Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Simple member function question

Simple member function question

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stephen McGeown
    wrote on last edited by
    #1

    Hi there, Supposing I have two header files called header1.h and header2.h - and I have two sourcefiles called source1.cpp and source2.cpp, which contain the constructors etc for classes declared in the respective header files. Now, I want to call a public member function from source2.cpp that was declared in header1.h. Obviously I can do this OK from source1.cpp but I can't do the same from source2.cpp. I realise this is a basic question but I'm a newbie. Any help (and examples) would be greatly appreciated. I've had a good old hunt around my books and MSDN but can't find a solution. E.g. Header1.h --------- class A { public: void member_function(); } source2.cpp ----------- #include "Header1.h" ... ... void calling_function(void) { // this won't work member_function(); } Thanks in advance!

    T S 2 Replies Last reply
    0
    • S Stephen McGeown

      Hi there, Supposing I have two header files called header1.h and header2.h - and I have two sourcefiles called source1.cpp and source2.cpp, which contain the constructors etc for classes declared in the respective header files. Now, I want to call a public member function from source2.cpp that was declared in header1.h. Obviously I can do this OK from source1.cpp but I can't do the same from source2.cpp. I realise this is a basic question but I'm a newbie. Any help (and examples) would be greatly appreciated. I've had a good old hunt around my books and MSDN but can't find a solution. E.g. Header1.h --------- class A { public: void member_function(); } source2.cpp ----------- #include "Header1.h" ... ... void calling_function(void) { // this won't work member_function(); } Thanks in advance!

      T Offline
      T Offline
      Thomas Blenkers
      wrote on last edited by
      #2

      Stephen, you're thinking too plain C... You define a class A having a member function (also called method) member_function. Your calling_function will call a function member_function having on the global namespace, which doesn't exist. This will work: .... A myInstanceOfA; myInstanceOfA.member_function(); .... Or, if you have *very good* reasons to have static functions in your class: class AA { public: static void static_member_function(); }; then this will work also: .... AA::static_member_function(); .... Thomas

      1 Reply Last reply
      0
      • S Stephen McGeown

        Hi there, Supposing I have two header files called header1.h and header2.h - and I have two sourcefiles called source1.cpp and source2.cpp, which contain the constructors etc for classes declared in the respective header files. Now, I want to call a public member function from source2.cpp that was declared in header1.h. Obviously I can do this OK from source1.cpp but I can't do the same from source2.cpp. I realise this is a basic question but I'm a newbie. Any help (and examples) would be greatly appreciated. I've had a good old hunt around my books and MSDN but can't find a solution. E.g. Header1.h --------- class A { public: void member_function(); } source2.cpp ----------- #include "Header1.h" ... ... void calling_function(void) { // this won't work member_function(); } Thanks in advance!

        S Offline
        S Offline
        Stephen McGeown
        wrote on last edited by
        #3

        Thanks for the help guys, but still no luck. On declaring an instance of the class (in a different sourcefile than the one containing the constructor) as advised, I receive the 'Classname' : undeclared identifier error. I have included the header file containing the class's definition at the top of the sourcefile. Is there something obvious I'm missing? (apart from a brain). In case it helps, I'm using VC++6 and it's an App Wizard generated SDI project. Cheers, Stephen.

        M 1 Reply Last reply
        0
        • S Stephen McGeown

          Thanks for the help guys, but still no luck. On declaring an instance of the class (in a different sourcefile than the one containing the constructor) as advised, I receive the 'Classname' : undeclared identifier error. I have included the header file containing the class's definition at the top of the sourcefile. Is there something obvious I'm missing? (apart from a brain). In case it helps, I'm using VC++6 and it's an App Wizard generated SDI project. Cheers, Stephen.

          M Offline
          M Offline
          Malcolm McMahon
          wrote on last edited by
          #4

          What is looks like your missing is context. To call a member function(method) it must be called in association with an object of the class it belongs to. You declare, say class A { .. public: void member_function(void); ... }; you have to call it like this: { A anInstanceOfa( ... ); // create an instance of A ... anInstanceOfa.member_function(); ... } or maybe: A *ptrA = new A(.. ) ptrA->member_function(); the member_function is only meaningfull in the context of a specific object of type A.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups