【问题标题】:How to create and use a global class in C++如何在 C++ 中创建和使用全局类
【发布时间】:2022-01-10 04:03:38
【问题描述】:

我想创建一个必须包含硬件驱动程序属性的变量。这些属性必须在我的代码的一部分中定义(当前是 main.ccp 中的 app_main()),并将在不同的部分中使用(例如在 driverUser.cpp 中)。

我当前的解决方案旨在在 nimBLEdriver.h 中创建一个全局类 NimBLEGluer。我已经阅读了与此相关的各种帖子,但仍然没有成功。下面的代码显示了三个部分:类定义、在 main() 中设置一些测试变量以及在 driverUser 中评估变量。我可以在main.ccp中成功地将testPointer的初始值从13变为7,但是在driverUser中该值仍然是初始值(13)。

driverUser中的代码在main()中的值改变后执行。

当我不使用 NimBLEGluer NimBLEData 进行实例化时,我的编译器会报错;在这两个地方。

如何实现对 testPointer 的检索结果为 7 ?

(已经注释了一些行,因为它们会导致一些其他问题,稍后会受到攻击)

/*******************
*nimBLEdriver.h
********************/
class NimBLEGluer {
    
    public:
    //constructor
    //NimBLEGluer(); //causes a: ../main/main.cpp:564: undefined reference to `NimBLEGluer::setPointer(int)'
                   //in that line there is NO reference to this class at all!
    //destructor
    //virtual ~NimBLEGluer();  

    int testPointer = 123;  //initial value for testing
    /*
    void setPointer( int inp);
    int getPointer( void ){
        return testPointer;
        };
    */        
    }; 

//NimBLEGluer::NimBLEGluer() {}  //causes a: error: definition of implicitly-declared 'constexpr NimBLEGluer::NimBLEGluer()'

//NimBLEGluer::~NimBLEGluer() {}

extern NimBLEGluer NimBLEDAta;  //to make it globally accessible without intantiating it again ?? 
/************************************************************************************************************/

/***** in main.cpp, which has a #include "nimBLEdriver.h" *****/
  NimBLEGluer NimBLEData;
  printf("MAIN testPointer: %d\n", NimBLEData.testPointer); //result: 123
  NimBLEData.testPointer = 7;
  printf("MAIN testPointer: %d\n", NimBLEData.testPointer); //result: 7
  

 /***** in driverUser.cpp, which has a #include "nimBLEdriver.h" *****/ 
  NimBLEGluer NimBLEData; //needs to be here, but resets the value of testPointer
  printf("RtMidiOut testPointer: %d\n", NimBLEData.testPointer);  //result: 123

【问题讨论】:

  • 您应该在整个程序的一个 .cpp 文件中包含 NimBLEGluer NimBLEData。您的头文件包含extern NimBLEGluer NimBLEDAta,这正是您需要在其他 .cpp 文件中引用它的内容。不需要其他声明。
  • 这是一个有用的见解!谢谢。有没有办法在多个 cpp 的代码段之间“传输”信息?
  • 我不确定你的意思。如果您想在翻译单元之间共享数据,那么是的,每次您使用 NimBLEData 它都引用同一个对象,这要感谢 extern NimBLEGluer NimBLEDAta 声明。更好的方法是避免使用全局变量,而是将数据作为函数参数传递。
  • 我同意您对使用全局值的评论,并将寻找一种为我的用例设计功能的方法。在我的示例中,我有三个(或 2 个和一个头文件?取决于定义)翻译单元。我现在在所有三个地方都添加了 extern。现在我得到一个:esp-idf/main/libmain.a(main.cpp.obj):(.literal.app_main+0x0): undefined reference to `NimBLEData'。
  • 好的,我将您的回答解释为:当我使用 extern 时,可以在翻译单元(不同的 .cpp's )之间共享数据。这是正确的解释吗?当我只在头文件中而不是在两个 cpp 中都有 extern 时,我就会遇到我的示例中的情况,这并没有给出预期的结果。因此,我尝试在两个 cpp 中添加 extern。你的建议是什么意思?

标签: c++ class


【解决方案1】:

感谢@n.1.8e9-where's-my-share m。我只能在一个翻译单元中使用 NimBLEGluer NimBLEData。最后,我从 driverUser.cpp 中删除了这一行。删除 nimBLEdriver.h (NimBLEDAta >>> NimBLEData) 中的 TYPO 后,我可以在 driverUser 中访问和打印 testPointer,而无需在该翻译单元中实例化对象。

所以:以这种方式在不同 .cpp 中的代码之间共享数据是可能的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    相关资源
    最近更新 更多