【问题标题】:This declaration has no storage class or type specifier when Initializing static member初始化静态成员时,此声明没有存储类或类型说明符
【发布时间】:2021-12-18 10:51:28
【问题描述】:

这是我的代码。

lib.hpp

class MainMenuDriver {
public:
    static LiquidCrystal lcd;
    static std::vector<std::string> menu_items;
    static std::stack<std::string>  menu_stack;
    static std::stack<std::string>  temp_stack;
    // ...

main.cpp

#include "lib.hpp"

MainMenuDriver::lcd = LiquidCrystal(8, 9, 4, 5, 6, 7); // Error
// ...

假设LiquidCrystal 确实存在,正如您在此处看到的,我想将蓝图和操作分成两个不同的部分。我已经知道你可以在MainMenuDriver 的构造函数中初始化lcd,但是我想以静态方式调用这个构造函数,这意味着我不想创建 MainMenuDriver 的实例。

提前谢谢大家,祝大家有个愉快的一天!

【问题讨论】:

    标签: c++ oop constructor static


    【解决方案1】:

    您还必须在初始化静态数据成员lcd 时指定类型,如下所示。使用

    LiquidCrystal MainMenuDriver::lcd = LiquidCrystal(8, 9, 4, 5, 6, 7);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多