public:静态常量字符串声明/初始化问题

时间:2018-07-19 05:28:43

标签: c++ string c++11 c++14 c++17

我希望创建静态常量字符串变量协议列表,但与此同时我也遇到了问题。

  • 构建系统:Visual Studios 2017
  • 开发/目标体系结构:x64
  • 开发/目标操作系统:Windows 10 Professional(64位)
  • 应用程序开发平台:Win32 API(Windows SDK 10.0.17134.0)

错误:

  

错误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_");

问题:

  1. 这是C ++ 17标准中引入的东西还是Visual C ++编译器强制引入的东西?我以前曾使用C ++ 14在gcc中编写代码,其中我在类中使用头文件中的public access修饰符定义了静态常量字符串变量,并在单独的源文件中对其进行了初始化。
  2. 除内联关键字之外,我还能通过哪些其他方式实现相同目标?

1 个答案:

答案 0 :(得分:3)

您错过了定义中的类名和const关键字

const std::string Protocol::libraryVersionRequestStr = std::string("GetLibraryVersion");

等等