我在Qt5开发并想要一些建议。
我想在一个类中声明const变量,并在其他类中使用此变量。 最好的方法是什么?
现在我的代码出错:
protocolcommands.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 "";
}
engine.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();
}
}
}
答案 0 :(得分:3)
我想在一个类中声明const变量,并在其他类中使用此变量。
这些字符串不属于您的ProtocolCommands
类;他们恰巧在那个竞争单位宣布。
如果你想让它们成为类的(静态)成员,那么在protocolcommands.h中ProtocolCommands
类的声明中声明它们:
class ProtocolCommands {
public:
static const QString CNT_FIRMWARE_ACK;
// ...
};
然后在protocolcommands.cpp中初始化它们:
const QString ProtocolCommands::CNT_FIRMWARE_ACK = "ACK";
然后你可以在名为ProtocolCommands::CNT_FIRMWARE_ACK
的包含protocolcommands.h的任何代码中引用它。
答案 1 :(得分:1)
我会使用enum
类型而不是将常量声明为字符串来执行此操作。因此,ProtocolCommands
类看起来像:
class ProtocolCommands
{
public:
enum Type {
CNT_FIRMWARE_ACK,
CNT_FIRMWARE_NAK,
CNT_FIRMWARE_NYET,
CNT_FIRMWARE_STALL,
CNT_FIRMWARE_HEARTBEAT
};
ProtocolCommands::Type parseCommand(const QByteArray &ba);
[..]
};
因此,您的ProtocolCommands::parseCommand()
应该返回一个整数值而不是字符串。因此,您的Engine::onSerialArrivedMsg(QByteArray msg)
函数将如下所示:
void Engine::onSerialArrivedMsg(QByteArray msg)
{
ProtocolCommands pc;
[..]
switch (pc.parseCommand(msg)) {
case ProtocolCommands::CNT_FIRMWARE_ACK:
[..]
case ProtocolCommands::CNT_FIRMWARE_NYET:
[..]
}
}
使用这种方法的好处是:
答案 2 :(得分:0)
在.cpp
文件中定义您的常量,或将pc.parseCommand(msg) == CNT_FIRMWARE_HEARTBEAT
个文件包含为附加(不良气味样式)。然后你可以将它们称为常量,如
<li id="contact|NoRelationship|Form|Mscrm.Form.contact.MainTab.ExportData" class="ms-cui-group" unselectable="on">
<span class="ms-cui-groupContainer" unselectable="on">
<span class="ms-cui-groupBody" unselectable="on">
<span id="contact|NoRelationship|Form|Mscrm.Form.contact.MainTab.ExportData-LargeMedium" class="ms-cui-layout" unselectable="on">
<span id="contact|NoRelationship|Form|Mscrm.Form.contact.MainTab.ExportData-LargeMedium-0" class="ms-cui-section" unselectable="on">
<span id="contact|NoRelationship|Form|Mscrm.Form.contact.MainTab.ExportData-LargeMedium-1" class="ms-cui-section" unselectable="on">
<span id="contact|NoRelationship|Form|Mscrm.Form.contact.MainTab.ExportData-LargeMedium-1-0" class="ms-cui-row" unselectable="on">
<a id="contact|NoRelationship|Form|fmc.contact.form.Button.Customer-Medium" class="ms-cui-ctl-medium " unselectable="on" href="javascript:;" onclick="return false;" aria-describedby="contact|NoRelationship|Form|fmc.contact.form.Button.Customer_ToolTip" mscui:controltype="Button" role="button">
<span class="ms-cui-ctl-iconContainer" unselectable="on">
<span class=" ms-cui-img-16by16 ms-cui-img-cont-float" unselectable="on">
<img unselectable="on" alt="" src="/%7B635732422790004376%7D/WebResources/fmc_/Images/HP.FMC.MSS360.CRM.Image.UpdateCustomer16X16">
</span>
</span>
<span class="ms-cui-ctl-mediumlabel" unselectable="on">Update Customer</span>
</a>
</span>
这是因为您只包含头文件,但标题不知道您要使用的常量。