我希望创建静态常量字符串变量协议列表,但与此同时我也遇到了问题。
错误:
错误LNK2001无法解析的外部符号“ public:静态类 std :: basic_string
,类 std :: allocator > const Protocol :: serviceVersionRequestStr“ (?serviceVersionRequestStr @ Protocol @@ 2V?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ B)
Protocol.h
class Protocol
{
public:
static const std::string libraryVersionRequestStr;
static const std::string serviceVersionRequestStr;
static const std::string libraryVersionResponseStr;
static const std::string serviceVersionResponseStr;
static const std::string restartStr;
static const std::string identifyUserStr;
static const std::string registerUserStr;
static const std::string deleteUserStr;
static const std::string identifyUserSuccessStr;
static const std::string identifyUserWithdrawStr;
static const std::string identifyUserwithdrawSuccessStr;
static const std::string identifyUserwithdrawFailureStr;
static const std::string positiveAcknowledgementStr;
static const std::string negativeAcknowledgementStr;
//Some Public and Private methods
}
Protocol.cpp
std::string libraryVersionRequestStr = std::string("GetLibraryVersion");
std::string serviceVersionRequestStr = std::string("GetServiceVersion");
std::string libraryVersionResponseStr = std::string("LibraryVersion:");
std::string serviceVersionResponseStr = std::string("ServiceVersion:");
std::string restartStr = std::string("RestartService");
std::string identifyUserStr = std::string("IndentifyUser");
std::string registerUserStr = std::string("RegisterUser");
std::string deleteUserStr = std::string("DeleteUser");
std::string identifyUserSuccessStr = std::string("IdentifyUserSuccess:");
std::string identifyUserWithdrawStr = std::string("IdentifyUserWithdraw");
std::string identifyUserwithdrawSuccessStr = std::string("IdentifyUserWithdrawSuccess:");
std::string identifyUserwithdrawFailureStr = std::string("IdentifyUserWithdrawFailure:");
std::string positiveAcknowledgementStr = std::string("Ack_");
std::string negativeAcknowledgementStr = std::string("Nack_");
如果我在定义过程中尝试初始化静态常量字符串,则会收到错误消息
例如:static const std::string negativeAcknowledgementStr = std::string("Nack_");
错误(活动)E1591类型“ const std :: string”的成员不能具有 内置初始化器
对于这个项目,我遵循C ++ 17标准,因此我通过添加inline
关键字并使用定义初始化变量来解决了该错误。
static inline const std::string negativeAcknowledgementStr = std::string("Nack_");
问题:
答案 0 :(得分:3)
您错过了定义中的类名和const关键字
const std::string Protocol::libraryVersionRequestStr = std::string("GetLibraryVersion");
等等