C/C++ C++ How to invoke an object's method in a different file

  • Thread starter Thread starter Bob Walance
  • Start date Start date
  • Tags Tags
    C++ programming
AI Thread Summary
The discussion revolves around the challenge of accessing a method from a class object defined in one source file (timer_functions.cpp) within another file (interrupts.cpp). The user created an instance of the Timers class but encountered build errors when trying to call its method from interrupts.cpp. The main issue identified is the improper declaration and linking of the object across files. Suggestions include using a static instance of the Timers class to avoid the need for creating and destroying the object with each interrupt, which simplifies the process. The solution of using a single global instance was confirmed as effective for the user's application, resolving the initial problem.
Bob Walance
Insights Author
Gold Member
Messages
82
Reaction score
55
TL;DR Summary
In C++, how to run a method from an object that was created in a separate file.
I have created an object for the class Timers in timer_functions.cpp, and I need to run a method associated with that object ( test_method() ) from interrupts.cpp.

I've tried many things but have been unsuccessful. Any ideas would be appreciated. Here is some sample code:

classes.hpp
C++:
   #pragma once
   class Timers {
   public:
     void test_method(void);
   private:
     int testVariable;
   };

classes.cpp
C++:
#include classes.hpp
   void Timers::test_method(void) {
     testVariable++;
   }

timer_functions.cpp
C++:
#include classes.hpp
   Timers myTimerObject; // create the object
interrupts.cpp
C++:
#include classes.hpp
   extern Timers myTimerObject; // this doesn't eliminate the build error
   void timer_interrupt(void) {
     myTimerObject.test_method(); // trying to run the objects method, but BUILD ERROR
   }
 
Last edited by a moderator:
Technology news on Phys.org
I'm not clear on what you are trying to do, and how things are split up. Normally, if you have a class foo, there is a header foo.hh and the actual code in foo.cc. If I have code bar.cc, it includes foo.hh, and the linker links foo.o and bar.o.

I don't see how your snippets match to this, but you seem to be creating an instance of an object in one file and using it in another. That shouldn't work - it shouldn't even compile, because the object isn't even declared in the second file. (And if it is declared, how does the compiler know it is supposed to be the same as in the first?)

Normally code would create the object and pass it to other code through a pointer to that object. The same code that created it should destroy it when it is no longer needed,
 
Vanadium 50 said:
I'm not clear on what you are trying to do, and how things are split up. Normally, if you have a class foo, there is a header foo.hh and the actual code in foo.cc. If I have code bar.cc, it includes foo.hh, and the linker links foo.o and bar.o.

I don't see how your snippets match to this, but you seem to be creating an instance of an object in one file and using it in another. That shouldn't work - it shouldn't even compile, because the object isn't even declared in the second file. (And if it is declared, how does the compiler know it is supposed to be the same as in the first?)

Normally code would create the object and pass it to other code through a pointer to that object. The same code that created it should destroy it when it is no longer needed,
I don't want to create and destroy the instance for each periodic timer interrupt. While this would probably work, it seems messy.

A pointer to the instance could be created in timer_functions.cpp, but how would it get passed to the timer interrupt?
 
Bob Walance said:
I don't want to create and destroy the instance for each periodic timer interrupt. While this would probably work, it seems messy.

A pointer to the instance could be created in timer_functions.cpp, but how would it get passed to the timer interrupt?
If you intend to have only a single (global) instance of Timers, then you can use a "static" instance. In that case, read up on static class variables and static member fns.

Of course, if you do indeed intend that, then why call it "Timers" (plural) instead of "Timer" singular?
 
  • Like
Likes Vanadium 50 and Bob Walance
strangerep said:
If you intend to have only a single (global) instance of Timers, then you can use a "static" instance. In that case, read up on static class variables and static member fns.
Wow. That works!

Yes, a single instance of the class (really it's no instance) is just fine for my application.

Thank you for the simple solution.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top