c++ - 公共(public) : static constant string declaraion/initialization issue

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

我希望创建静态常量字符串变量协议(protocol)列表,但我遇到了与此相关的问题。

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

错误:

Error LNK2001 unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > 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_");

Error (active) E1591 a member of type "const std::string" cannot have an in-class initializer

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

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

问题:

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

最佳答案

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

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

等等

关于c++ - 公共(public) : static constant string declaraion/initialization issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51414962/

相关文章:

c++ - std::shared_ptr 和 std::experimental::atomic_shared_ptr 有什么区别?

C++ 模板专门化,使用 typename 时出错

c++ - 类中的引用对象和其他引用对象

c++ - std::for_each 在不可复制对象的 vector 上

c++ - GCC 使用旧版本的 C++

java.lang.ArrayIndexOutOfBoundsException 和 If 逗号后面的条目不是整数

python - 为什么变量1 + =变量2要比变量1 =变量1 +变量2快得多?

c++ - 在输入流中搜索字符串

c++ - SDL 程序卡住

java - java中字符串分割的问题(Android)