mercredi 25 mars 2015

Linker error when passing static const int as reference [duplicate]


This question already has an answer here:




Why do I get undefined reference errors to NUMBER when compiling this with gcc 4.6.3?:



g++ -std=c++0x -pipe -Wall -pedantic file.c


file.c:



#include <stdio.h>
#include <utility>

class Thing {
public:
Thing() {
foo(NUMBER);
const int* p = &NUMBER;
printf("%d\n", *p);
}
void foo(const int& i) {}
static const int NUMBER = 123;
};

int main() { Thing thing; return 0; }


Error:



chetic@home:~/Documents$ g++ -std=c++0x -pipe -Wall -pedantic test.c
/tmp/cceBY2zr.o: In function `Thing::Thing()':
statest.c:(.text._ZN5ThingC2Ev[_ZN5ThingC5Ev]+0x11): undefined reference to `Thing::NUMBER'
statest.c:(.text._ZN5ThingC2Ev[_ZN5ThingC5Ev]+0x21): undefined reference to `Thing::NUMBER'


I have been referred to #3 here: http://ift.tt/1IuYStb But I was hoping somebody could explain it in simpler terms.


Why doesn't the compiler give a readable error that slaps me on my hands, or alternatively, why doesn't it treat NUMBER as a variable?


Also, why does this appear as a linker error?


Aucun commentaire:

Enregistrer un commentaire