Reputation: 8956
One would think that this simple piece of code should compile easily:
#include <utility>
struct Q {
static const int X = 0;
};
int main() {
std::pair<int, int>(Q::X, 0);
return 0;
}
However, when I compile this with g++
using the default settings (cygwin gcc 4.5.3) it throws a linker error at me:
undefined reference to `Q::X'
I'm totally stumped here -- MSVC compiles this just fine yet my gcc does not. (MinGW also works fine.)
Upvotes: 3
Views: 2123
Reputation: 1156
@Maxim Yegorushkin (Beat me to it!) Here is a relevant link that might help.
But your problem is that the int is never externally defined. If X doesn't have to be const, you can initialize it outside the struct in a similar manner. The main reason is because X is static...
struct Q {
static const int X;
};
int const Q::X = 0;
struct Q {
static int X;
};
int Q::X = 0;
Upvotes: 1
Reputation: 1276
You only declare X but you must also define it, which, for static definitions must happen outside the class
struct Q {
static const int X = 0;
};
int Q::X = 0;
Upvotes: 1
Reputation: 136208
The linker fails to find definition of Q::X
.
This is probably because std::pair<>
constructor takes arguments as references to const, and Q::X
is an l-value, hence it requires an external definition of Q::X
to be able to refer to that.
A portable fix:
int const Q::X;
Upvotes: 3