Author Topic: C++ programming How to access a variable from outside  (Read 2726 times)

0 Members and 1 Guest are viewing this topic.

Offline JesterTopic starter

  • Frequent Contributor
  • **
  • Posts: 859
  • Country: ca
Re: C++ programming How to access a variable from outside
« Reply #25 on: October 07, 2022, 02:51:07 pm »
You could just pass a reference to core as an argument to newFunction():

Code: [Select]
class ControlPanel {
  [...]
public:
  void newFunction(Core &core);
};

This way you can call panel.newFunction(core) from main(), where you have core available. You will have to include Core.h in ControlPanel.h (I'm guessing at file names here) so the compiler knows what Core is.

It would be helpful if you posted your code.

This seems like a good approach, however

public:
  void newFunction(Core &core);       <-- I get the compiler error "identifier "Core" is undefined"  for this line that I added to ControlPanel.h

This is with #include "Core.h"       in ControlPanel.h  just under the other #includes for example SPIBus.h at the top of ControlPanel.h

Code: [Select]
#include "F28x_Project.h"
#include "SPIBus.h"
#include "Core.h"           // new Oct 6

Code: [Select]


« Last Edit: October 07, 2022, 03:04:35 pm by Jester »
 

Offline hubi

  • Regular Contributor
  • *
  • Posts: 57
  • Country: us
Re: C++ programming How to access a variable from outside
« Reply #26 on: October 07, 2022, 03:20:58 pm »
Look at the includes in main.cpp, where you previously used Core without compiler errors to see what else is included there. Also check to make sure Core is not inside a different namespace. Does your main program have "using namespace" anywhere? Is there a namespace in Core.h? If it is a namespace issue, you can change the function to:

Code: [Select]
class ControlPanel {
  [...]
public:
  void newFunction(mynamespace::Core &core);
};
Replace "mynamespace" above with the correct name for the namespace.

 

Offline hubi

  • Regular Contributor
  • *
  • Posts: 57
  • Country: us
Re: C++ programming How to access a variable from outside
« Reply #27 on: October 07, 2022, 03:35:21 pm »
Ignore my previous reply. I downloaded the code and it is not a namespace issue. The problem is that Core.h includes ControlPanel.h. I don't see a reason why it is included, you can try to remove it from "Core.h" and see if that fixes it. If not, remove the #include "Core.h" from ControlPanel.h and instead just add the following line:

Code: [Select]
class Core;

You have to include "Core.h" in ControlPanel.cpp where you implement ControlPanel::newFunction();

 
The following users thanked this post: Jester

Offline JesterTopic starter

  • Frequent Contributor
  • **
  • Posts: 859
  • Country: ca
Re: C++ programming How to access a variable from outside
« Reply #28 on: October 07, 2022, 05:46:34 pm »
Ignore my previous reply. I downloaded the code and it is not a namespace issue. The problem is that Core.h includes ControlPanel.h. I don't see a reason why it is included, you can try to remove it from "Core.h" and see if that fixes it. If not, remove the #include "Core.h" from ControlPanel.h and instead just add the following line:

Code: [Select]
class Core;

You have to include "Core.h" in ControlPanel.cpp where you implement ControlPanel::newFunction();

hubi,

THANK YOU..... adding class Core to ControlPanel.h did the trick

I would like to understand why. 

1) I guess without 
Code: [Select]
class  Core  the ControlPanel class and hence its member functions don't know anything about the Core class, and adding 
Code: [Select]
class  Core then provides that definition to ControlPanel?

2) Then when I call controlPanel.newFunction(core);  in main it uses the actual instance "core" represented by the Core class, is that correct?
 

Offline hubi

  • Regular Contributor
  • *
  • Posts: 57
  • Country: us
Re: C++ programming How to access a variable from outside
« Reply #29 on: October 07, 2022, 06:41:05 pm »
1) I guess without 
Code: [Select]
class  Core  the ControlPanel class and hence its member functions don't know anything about the Core class, and adding 
Code: [Select]
class  Core then provides that definition to ControlPanel?
It's not so much that ControlPanel needs to know what Core is, but the compiler needs to know.

2) Then when I call controlPanel.newFunction(core);  in main it uses the actual instance "core" represented by the Core class, is that correct?
Yes, it uses a reference to the instance of class Core.

 
The following users thanked this post: Jester


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf