Simple member function question
-
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!
-
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!
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
-
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!
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.
-
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.
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.