I'm trying to create a reference variable cParamIDsByAge
to one side of a boost::bimap
for convenience (MVCE):
Attempt #1
code.h
#include <boost/bimap.hpp>
typedef boost::bimap <int, int> ParamIDs;
extern const ParamIDs cParamIDs;
extern auto &cParamIDsByAge = cParamIDs.left;
code.cpp
#include "code.h"
// In my code this inits the variable with some data via a function.
const ParamIDs cParamIDs {};
auto &cParamIDsByAge = cParamIDs.left;
The compiler complains:
code.h:5:14: warning: ‘cParamIDsByAge’ initialized and declared ‘extern’
extern auto &cParamIDsByAge = cParamIDs.left;
^~~~~~~~~~~~~~
code.cpp:4:7: error: conflicting declaration ‘auto& cParamIDsByAge’
auto &cParamIDsByAge = cParamIDs.left;
^~~~~~~~~~~~~~
In file included from code.cpp:1:0:
code.h:5:14: note: previous declaration as ‘const boost::bimaps::views::map_view<boost::bimaps::relation::member_at::left, boost::bimaps::detail::bimap_core<int, int, mpl_::na, mpl_::na, mpl_::na> >& cParamIDsByAge’
extern auto &cParamIDsByAge = cParamIDs.left;
^~~~~~~~~~~~~~
Attempt #2
Then I tried defining the reference cParamIDsByAge
in the header directly:
code.h
#include <boost/bimap.hpp>
typedef boost::bimap <int, int> ParamIDs;
extern const ParamIDs cParamIDs;
auto &cParamIDsByAge = cParamIDs.left;
code.cpp
#include "code.h"
const ParamIDs cParamIDs {};
But that complained about:
error: multiple definitions of cParamIDsByAge
Attempt #3
I don't initialized the reference in the header:
code.h
#include <boost/bimap.hpp>
typedef boost::bimap <int, int> ParamIDs;
extern const ParamIDs cParamIDs;
extern const auto &cParamIDsByAge;
code.cpp
#include "code.h"
const ParamIDs cParamIDs {};
auto &cParamIDsByAge = cParamIDs.left;
Compiler complains about header file:
error: declaration of ‘const auto& cParamIDsByAge’ has no initializer