I have created a header mongo.h it contain
mongocxx::instance inst{};
mongocxx::uri uri{ "link*****" };
mongocxx::client client{ uri };
i accessed the mongodb from main.cpp by including this
mongo.h
but when including this header to other cpp file it return error.
Documents saying that the instance must create once . i have read http://mongocxx.org/api/current/classmongocxx_1_1instance.html not understand fully, i dont familiar with constructor and destructor ,, Any body please help to access mongodb from every cpp file .
This is a good example of where a singleton could help. In
mongo.h, put a single function declaration:In a single cpp file, define the function as follows:
By putting this in a separate .cpp file, you ensure that
instis created some time before themainfunction starts. Thestatickeyword inget_clientensures that the client is only ever created once, namely the first timeget_clientis called.