c++ - 在 Qt 中声明 const 并从 2 个不同的类访问

标签 c++ qt

我在 Qt5 中开发并需要一些建议。

我想在一个类中声明 const 变量并在其他类中使用该变量。 最好的方法是什么?

现在我的代码出现错误:

协议(protocol)命令.cpp

const QString CNT_FIRMWARE_ACK = "ACK";
const QString CNT_FIRMWARE_NAK = "NAK";
const QString CNT_FIRMWARE_NYET = "NYET";
const QString CNT_FIRMWARE_STALL = "STALL";
const QString CNT_FIRMWARE_HEARTBEAT = "HEARTBEAT";

QString ProtocolCommands::parseCommand(QByteArray ba)
{

    QString s;
    if (ba[2] == 0x02)
    {
        s = "flow control";
        if (ba[3] == 0x01 && ba[4] == 0x01)
          return CNT_FIRMWARE_ACK;
        if (ba[3] == 0x01 && ba[4] == 0x02)
          return CNT_FIRMWARE_NAK;
        if (ba[3] == 0x01 && ba[4] == 0x03)
          return CNT_FIRMWARE_STALL;
        if (ba[3] == 0x01 && ba[4] == 0x04)
          return CNT_FIRMWARE_NYET;
    }
    else if (ba[2] == 0x03)
    {
        s = "data packet";
        qDebug() << ba.toHex();
        if (ba[3] == 0x1a && ba[4] == 0x02)
         return "getJsonStringFor_GetStimulatorOperaionType";

    }
    else if (ba[2] == 0x04)
    {
          return CNT_FIRMWARE_HEARTBEAT;
    }
   // return "";
}

引擎.cpp:

void Engine::onSerialArrivedMsg(QByteArray msg)
{
ProtocolCommands pc;
qDebug() << "somthing has arrived : " << msg.toHex();
//if we here - this is new message from the firmware
//this function refer just to one message at a time

//first check if we are in the middle of flow:
if (mode)
{
     qDebug() << "mode = true";
     switch (curr_step_in_flow) {
     case 1:  //last command send to FW was [Set SinglePulse Parameters]
         if (pc.parseCommand(msg) == ProtocolCommands::CNT_FIRMWARE_ACK)    // compile eror
         {
            qDebug() << "ack recieved";
            curr_step_in_flow = 2;
         }
         else if (pc.parseCommand(msg) == CNT_FIRMWARE_HEARTBEAT ) // compile error
         {
             //heartbeat msg
             //send signal to the heartbeat thread
             emit fwHeartBeatArrived();
         }
         break;
     default:
         break;
     }

}
else
{
   if (pc.parseCommand(msg) == CNT_FIRMWARE_HEARTBEAT)  // compile eror
   {
       //heartbeat msg
       //send signal to the heartbeat thread
       emit fwHeartBeatArrived();
   }

}

}

最佳答案

I want to declare const variable in one class and use this variable in other classes.

那些字符串不是您的 ProtocolCommands 类的一部分;它们恰好在那个编译单元中被声明。

如果您希望它们作为类的(静态)成员,则在 protocolcommands.h 中的 ProtocolCommands 类的声明中声明它们:

class ProtocolCommands {
public:
   static const QString CNT_FIRMWARE_ACK;
   // ...
};

然后在protocolcommands.cpp中初始化它们:

const QString ProtocolCommands::CNT_FIRMWARE_ACK = "ACK";

然后您可以在包含 protocolcommands.h 的任何代码中通过名称 ProtocolCommands::CNT_FIRMWARE_ACK 引用它。

关于c++ - 在 Qt 中声明 const 并从 2 个不同的类访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31584596/

相关文章:

c++ - 使用arduino键盘编译错误 "Event listener"

c++ - "I' m busy please wait"窗口 - 没有按钮

c++ - 复制图像后在 imshow 中断言失败 (size.width>0 && size.height>0)

c++ - 正确终止一个 QThread

qt - 根据 QtQuick 2.0 中的属性查找 ListModel 的特定元素

c++ - Qt 创建者。更改插槽的代码生成模板

c++ - 在 WinAPI 函数中使用 std::wstring:并非所有文本都已发送

c++ - Qt中如何添加自定义类处理功能

c++ - Qt 项目中的 unique_ptr

c++ - WinEventHook 不捕获来自特定进程 ID 的事件