bowzee bowzee - 5 months ago 26
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?


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.