代码之家  ›  专栏  ›  技术社区  ›  Dark Sorrow

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

  •  0
  • Dark Sorrow  · 技术社区  · 8 年前

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

    • 构建系统:视觉工作室2017
    • 开发/目标架构:x64
    • 开发/目标操作系统:Windows10专业版(64位)
    • 应用程序开发平台:Win32 API(Windows SDK 10.0.17134.0)

    错误:

    错误LNK2001未解析的外部符号“public:静态类” STD::Basic字符串Stlt;char,StRtSTD::CARYMARGIONS;CHAR & GT;类 STD::分配器& lt;char & gt;& const协议::Service Exvices请求语句 ?serviceversionrequeststr@protocol@@2v?$basic_string@du?$char_traits@d@std@@v?$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_");
    

    如果我试图在定义期间初始化静态常量字符串,我会得到一个错误

    例如: static const std::string negativeAcknowledgementStr = std::string("Nack_");

    错误(活动)E1591:“const STD::字符串”类型的成员不能拥有 类内初始值设定项

    对于这个项目,当我坚持C++ 17标准时,我通过添加 inline 关键字,并使用定义初始化变量。

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

    问题:

    1. 这是在C++ 17标准中引入的东西还是被强制的Bug Visual C++编译器?我以前在GCC中使用C++ 14编写了代码,在这里我用一个头文件中的公共访问修饰符在类中定义了静态常量字符串变量,并将它们初始化在单独的源文件中。
    2. 我可以用什么其他方法达到同样的效果(除了内联关键字) ?
    1 回复  |  直到 8 年前
        1
  •  3
  •   john    8 年前

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

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

    等等。