I am trying to define a global map within a class a, then use it in class b. although map is not empty, I could not see the elements.
another issue that bother me is, how do I know if my code compiled using c++11 standard? How can I set it, to compile with c++98 or C++2003 ?
I have 5 files:
map_user_a.h
#include <map> #include <string> #include <stdint.h> using namespace std; extern std::map<string, uint8_t> map_global; class map_user_a { public: void init(); private: int user_a; };
map_user_a.cpp
#include <iostream> #include "map_user_a.h" std::map<string, uint8_t> map_global; void map_user_a::init() { std::cout<<"map_user_a::init()"<<endl; map_global.insert(std::make_pair("1", 010)); map_global.insert(std::make_pair("2", 020)); }
map_user_b.h
#include <map> #include <string> #include <stdint.h> using namespace std; extern std::map<string, uint8_t> map_global; class map_user_b { public: void use(); private: int user_b; };
map_user_b.cpp
#include <iostream> #include "map_user_b.h" using namespace std; void map_user_b::use() { cout<<"map_user_b::use()"<<endl; cout<<"global map first pair="<< map_global["1"] <<endl; }
main.cpp
#include "map_user_a.h" #include "map_user_b.h" main() { map_user_a map_a; map_a.init(); map_user_b map_b; map_b.use(); }
Now, I am compile the code under ubuntu 17.10.1 using cmake.
CMakeLists.txt is
cmake_minimum_required(VERSION 3.9)
project (stl)
add_executable(stl map_user_a.cpp map_user_a.h map_user_b.cpp map_user_b.h main.cpp)
after call to cmake ../build (build is my build directory) I am calling to make, the code compiled with no errors.
But my output is first pair empty although the map has element.
map_user_a::init()
map_user_b::use()
global map first pair=
what is wrong ? Thanks.