吸气剂静态矢量

Ste*_*nov 2 c++ static stdvector visual-studio

我上课了.但是当我为它添加静态向量和getter时,我遇到了编译错误.

这是一个例子:

// Config.h file
class Config {
    static std::vector<std::string> m_RemoteVideoUrls;
    ...
public:
    static std::vector<std::string> GetRemoteVideoURLs();
};


// Config.cpp file
static std::vector<std::string> m_RemoteVideoUrls = {"some url"};
...
std::vector<std::string> Config::GetRemoteVideoURLs() {
    return m_RemoteVideoUrls;
}
Run Code Online (Sandbox Code Playgroud)

我在Visual Studio 2017中编译期间遇到了这个奇怪的错误

1>config.obj : error LNK2001: unresolved external symbol "private: static class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > Config::m_RemoteVideoUrls" (?m_RemoteVideoUrls@Config@@0V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@A)
Run Code Online (Sandbox Code Playgroud)

经过几次实验,我明白了我的错误m_RemoteVideoUrls.因为这个存根工作:

std::vector<std::string> Config::GetRemoteVideoURLs() {
    return std::vector<std::string>{"a", "b"};// m_RemoteVideoUrls;
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

std::vector<std::string> Config::GetRemoteVideoURLs() {
    LoadConfigIfRequired();
    std::vector<std::string> tmp = m_RemoteVideoUrls;
    return std::vector<std::string>{"a", "b"};// m_RemoteVideoUrls;
}
Run Code Online (Sandbox Code Playgroud)

怎么了?

use*_*670 5

static std::vector<std::string> m_RemoteVideoUrls = {"some url"};
Run Code Online (Sandbox Code Playgroud)

只是一个不相关的全局变量,给它应该是一个静态成员的定义

std::vector<std::string> Config::m_RemoteVideoUrls = {"some url"};
Run Code Online (Sandbox Code Playgroud)