This must be very trivial, but I can't find it out:
struct Test {
static int n;
void Save(int val) {
Test::n = val;
}
};
int main() {
Test t;
t.Save(2);
return 0;
}
Why there is undefined reference to Test::n
at line 4?
You need to define the static:
struct Test {
static int n;
void Save(int val) {
Test::n = val;
}
};
int Test::n = 0;
Note that the definition must appear in an implementation file, not a header, otherwise you'll get a multiple definition error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With