C++ 公共:静态常量字符串声明/初始化问题

C++ 公共:静态常量字符串声明/初始化问题,c++,string,c++11,c++14,c++17,C++,String,C++11,C++14,C++17,我希望创建静态常量字符串变量协议列表,但我面临着同样的问题 构建系统:Visual Studio 2017 开发/目标架构:x64 开发/目标操作系统:Windows 10 Professional(64位) 应用程序开发平台:Win32 API(Windows SDK 10.0.17134.0) 错误: 错误LNK2001未解析外部符号“公共:静态类” std::基本字符串常量协议::serviceVersionRequestStr“ (?serviceVersionRequestStr@

我希望创建静态常量字符串变量协议列表,但我面临着同样的问题

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

错误LNK2001未解析外部符号“公共:静态类” std::基本字符串常量协议::serviceVersionRequestStr“ (?serviceVersionRequestStr@Protocol@@2V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@B)

方案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
}
协议.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_");
如果在定义期间尝试初始化静态常量字符串,则会出现错误

e、 g.:
static const std::string negativeacknowledgestr=std::string(“Nackè”)

错误(活动)E1591“const std::string”类型的成员不能具有 类内初始值设定项

对于这个项目,由于我遵循C++17标准,我通过添加
inline
关键字并使用定义初始化变量来解决这个错误

static inline const std::string     negativeAcknowledgementStr = std::string("Nack_");
问题:

这是C++ 17标准中引入的东西还是被强制的Bug Visual C++编译器?我以前使用C++14在gcc中编写代码,我在类中定义了静态常量字符串变量,在头文件中使用公共访问修饰符,并在单独的源文件中初始化它们
  • 除了内联关键字之外,还有什么其他方法可以达到同样的效果

  • 定义中缺少类名和const关键字

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

    等等。

    您在定义中遗漏了类名和const关键字

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

    等等。

    如果要将声明与初始化分开,则必须限定初始化中的变量以消除链接器错误,例如:
    std::string Protocol::libraryVersionRequestStr=“GetLibraryVersion”std::string Protocol::libraryVersionRequestStr=“GetLibraryVersion”等等。谢谢你指出我的错误,我浪费了很多时间想知道它为什么不起作用。谢谢你指出我的错误,我浪费了很多时间想知道它为什么不起作用。