Bonjour,
Quelqu'un pourrait m'expliquer pourquoi ce code ne se link pas avec g++/ld ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <cstddef>
class Pixel {
public:
static const unsigned char ERROR_CODE = 0;
static unsigned char getRed(const unsigned char* pixel);
};
inline unsigned char Pixel::getRed(const unsigned char* pixel) {
return (pixel == NULL) ? ERROR_CODE : pixel[0];
}
int main() {
Pixel::getRed(NULL);
} |
ld retourne l'erreur suivante :
1 2 3
| /tmp/ccUG68Zg.o: In function `Pixel::getRed(unsigned char const*)':
test.cc:(.text._ZN5Pixel6getRedEPKh[Pixel::getRed(unsigned char const*)]+0x1b): undefined reference to `Pixel::ERROR_CODE'
collect2: ld a retourné 1 code d'état d'exécution |
En revanche, si je remplace :
return (pixel == NULL) ? ERROR_CODE : pixel[0];
par
1 2 3 4
| if (pixel == NULL)
return ERROR_CODE;
else
return pixel[0]; |
le linker est content.
Ou alors autre alternative, on garde :
return (pixel == NULL) ? ERROR_CODE : pixel[0];
mais on transforme la constante en int au lieu d'un unsigned char.
J'avoue que pour moi ça relève du paranomal là .
Partager