【问题标题】:public: static constant string declaraion/initialization issue公共:静态常量字符串声明/初始化问题
【发布时间】:2018-07-19 05:28:42
【问题描述】:

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

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

错误:

错误 LNK2001 未解析的外部符号“公共:静态类 std::basic_string,class std::allocator > const Protocol::serviceVersionRequestStr" (?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::string”的成员不能拥有 类内初始化器

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

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

问题:

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

【问题讨论】:

  • 如果要将声明与初始化分开,则必须限定初始化中的变量以消除链接器错误,例如:std::string Protocol::libraryVersionRequestStr = "GetLibraryVersion"; 等等。

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


【解决方案1】:

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

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

等等。等等

【讨论】:

  • 感谢您指出我的错误,我浪费了很多时间想知道为什么它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多