我是c ++的初学者。我创建了名称空间并尝试初始化std::vector
个std::string
个对象,作为名称空间变量
namespace nsHttpWorker{
std::vector<string> nvMobileAgents = {
"Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
"Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
"Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9",
"Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; HTC_IncredibleS_S710e Build/GRJ90) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"Mozilla/5.0 (Linux; U; Android 2.3.4; fr-fr; HTC Desire Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile myTouch 3G Slide Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3",
"Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543 Safari/419.3",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A",
"Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25",
"Mozilla/5.0 (iPod touch; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4",
"Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4",
"Mozilla/5.0 (iPad; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4"
};
}
这给了我这样的错误:
worker.obj:-1: ошибка: LNK2005: "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> > > > nsFacebookHttp::nvMobileAgents"
(?nvMobileAgents@nsFacebookHttp@@3V?$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)
already defined in mainwindow.obj
所以不可能在c ++命名空间中创建容器初始化吗?
答案 0 :(得分:0)
所以不可能在c ++命名空间中创建容器初始化吗?
当然有可能。您收到链接器错误,告诉您,您有该向量变量的多个定义。
看起来你已经在另一个翻译单元中定义了该向量,假设你在多次包含的头文件中有这个,只需写:
namespace nsHttpWorker{
extern std::vector<string> nvMobileAgents;
}
并将定义(初始化)移动到单独的.cpp
文件中。