#include <string>
#include <iostream>
using namespace std;
struct foo {
static const int X = 3;
static char bar(const string &str) {
// return str[X]; // this works
return *(str.begin() + X); // this fails
}
};
int main() {
cout << foo::bar("abcdefg") << endl;
}
When compiling this, I get a linker error saying “undefined symbol foo::X”. If the previous line is uncommented instead, then it compiles. What makes it different between these two?
You need to define
foo::Xin addition to declaring it. Do it outside offoo.