lundi 28 août 2017

C++11 extern complex Container and Container initialization

When we want to have an exported variable (both in C and C++) we need to:

- declare the variable as extern in an hpp header file (i.e. extern int x;)

- initialize the variable in a cpp file (i.e.: int x = 0).

However, I have found difficulties with containers and I am receiving messages like "the variable is declared multiple times" and so forth.

For example:

hpp file:

typedef std::vector<std::pair<std::string, std::string>> VectorOfPairs_t;
export VectorOfPairs_t vectorOfPairs;

cpp file:

std::pair<std::string, std::string> myPair;
myPair = std::make_pair("hello", "world");
vectorOfPairs.push_back( myPair ); // All this is just a hack
                                   // to initialize the container...

main.cpp (or other cpp):

use of vectorOfPairs accordingly to my requirements

But, as I have mentioned, this is not compiling.

Could you please tell me what I am doing wrong?

Aucun commentaire:

Enregistrer un commentaire