bowzee bowzee - 1 year ago 95
C++ Question

C++ tuple linking issue: undefined reference

I suppose that there is something terribly wrong with this code. It will compile but does not get linked.

#include <iostream>
#include <tuple>

class Table_class
constexpr static std::tuple<int, unsigned int, unsigned short> table[3]
= {std::make_tuple(1, 2, 3),
std::make_tuple(4, 5, 6),
std::make_tuple(7, 8, 9)};

int main()
std::cout << std::get<0>(Table_class::table[0]);
return 0;

The error that shows up is this:

[31m/tmp/ccDiIuPv.o: In function `main':
file.cpp:(.text+0x5): undefined reference to `Table_class::table'
collect2: error: ld returned 1 exit status

Compilation Failed

How can this be corrected?

Answer Source

It's not terribly wrong. Your code is (will be) perfectly legal in C++17. However, before C++17, static constexpr data members need to be defined outside of class, so find somewhere and add the following definition:

constexpr std::tuple<int, unsigned int, unsigned short> Table_class::table[3];


As usual, variable definition should not be in a header file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download