I am creating a C++ 11 program which compiles but fails to link. I have traced the error (shown at the bottom of this post) to a single line of code:
m_equities[symbol] = temp;
where m_equities is defined as:
map<string, EquityInDB> m_equities;
and temp is an instance of EquityInDB.
Can someone explain why this one line of code is causing the linker error below? It looks like that one line is trying to create an instance of my EquityInDB class using the default constructor (there is none). My EquityInDB class requires parameters in the constructor.
(Note: Commenting out the one assignment line above lets everything compile)
g++ -o MetaStockDB main.o date.o tradingday.o equity.o metastockdb.o msfileio.o equityindb.o bytearray.o metastockdb.o: In function
std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, EquityInDB>::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, 0ul>(std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&>&, std::tuple<>&, std::_Index_tuple<0ul>, std::_Index_tuple<>)': Makefile:254: recipe for target 'MetaStockDB' failed /usr/include/c++/6.3.1/tuple:1586: undefined reference to
EquityInDB::EquityInDB()'
The
m_equities[symbol]
creates an elment using the default constructor, if there is no elment in the map for that key. So usingoperator[]
requires the default constructor to exists.You should use
insert
andstd::make_pair
instead.std::map operator_at:
and