1

Im a beginner in c++. I created namespace and tried to initialise the std::vector of std::string objects, as a namespace variable

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"
    };

}

This gave me the error like this:

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

So is it not possible to make container initalisations in c++ namespace?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Vlad
  • 369
  • 4
  • 16
  • Try something simpler first, line an `int`. – juanchopanza Jul 04 '16 at 16:21
  • @juanchopanza i tried like this `std::vector ls = {1,2,3,4};` as a namespace variable and i got following error: `facebookworker.obj:-1: ошибка: LNK2005: "class std::vector > nsFacebookHttp::ls" (?ls@nsFacebookHttp@@3V?$vector@HV?$allocator@H@std@@@std@@A) already defined in mainwindow.obj` – Vlad Jul 04 '16 at 16:24
  • Try something simpler first, line an `int`. Not vector of ints. Just an `int`. – juanchopanza Jul 04 '16 at 16:25

1 Answers1

0

So is it not possible to make container initalisations in c++ namespace?

Of course it's possible. You got a linker error telling you, that you have multiple definitions of that vector variable.

It looks like you have already defined that vector in another translation unit, Supposed you have that in a header file that is included multiple times, just write:

 namespace  nsHttpWorker{
     extern std::vector<string> nvMobileAgents;
 }

and move the definition (initialization) into a separate .cpp file.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thanks this is helped me! I made it like that in `.h` `extern std::vector nvMobileAgents;` and then in cpp `#include "facebookworker.h" namespace nsFacebookHttp{ std::vector nvMobileAgents = { "dsadasd" };` But why is that happens exactly? Because i did multiple includes of one file? – Vlad Jul 04 '16 at 16:40
  • @yasofiz It happens if you include the header multiple times from different translation units (`.cpp` files), and each of them contains the definition when these are linked together. – πάντα ῥεῖ Jul 04 '16 at 16:45