szym_rutkowski
szym_rutkowski

Reputation: 24

g++ throws "undefined reference to" static class member

I'm writing CCITT Group 4 encoder and it seems I have a problem with linker.

I have some raw binary data stored as static const class members (Fakskod.h):

class Fakskod {
  public:
  // ...
   static const unsigned char codesTerminateWhite[64][3];
   static const unsigned char codesTerminateBlack[64][3];
   static const unsigned char codesMakeUpWhite[40][3];
   static const unsigned char codesMakeUpBlack[40][3];

  private:
  //...
} ;

I had to initialize them elsewhere, because brace-enclosed initializers can't be used in class definition. Not to mention clearness of the header. So in another file (Fakskod-codes.cpp) I assigned data:

const unsigned char codesTerminateBlack[64][3] = { {0x0d, 0xc0, 0xa}, 
{0x40, 0x0, 0x3},  {0xc0, 0, 0x2}, {0x60, 0, 0x3},  {0x30, 0, 0x4},
{0x20, 0, 0x4}, {0x18, 0, 0x5},  {0x14, 0, 0x6},  {0x10, 0, 0x6},
//... (~200 hex bytes)
} ;

And that's what I got:

/tmp/ccm0ijoi.o: In function `Fakskod::makeCode(int, fkColor)':
rectangle_test.cpp:(.text+0x19b0): undefined reference to `Fakskod::codesTerminateWhite'
rectangle_test.cpp:(.text+0x1a02): undefined reference to `Fakskod::codesMakeUpWhite'
rectangle_test.cpp:(.text+0x1a7b): undefined reference to `Fakskod::codesTerminateWhite'
rectangle_test.cpp:(.text+0x1b0f): undefined reference to `Fakskod::codesTerminateBlack'
rectangle_test.cpp:(.text+0x1b5e): undefined reference to `Fakskod::codesMakeUpBlack'
rectangle_test.cpp:(.text+0x1bce): undefined reference to `Fakskod::codesTerminateBlack'
collect2: error: ld returned 1 exit status

It seems the source of trouble is this method (Fakskod-Horizontal.cpp):

std::vector<unsigned char> Fakskod::makeCode(int num, fkColor color) {

  #define FAKSKOD_MKCD(TERM, MKUP) \
  if(num < 64) \
    return vectorize( TERM [num]); \
  else { \
    for(int i=40; i>0; i--) \
       if(num/64 == i) { \
           code = vectorize( MKUP [i-1]); \
           num -= 64 * i; \
       } \
    return mergeStreams(code, vectorize( TERM [num])); \
  }

   std::vector<unsigned char> code;
   if(color==WHITE)    { FAKSKOD_MKCD(codesTerminateWhite, codesMakeUpWhite) }
   else                { FAKSKOD_MKCD(codesTerminateBlack, codesMakeUpBlack) }

} // Fakskod::makeCode

I'm sure that everything is ok with the macro. Preprocessor (g++ -E) resolves it correctly and when I paste expanded version to my code, the message is the same.

Also I tried to #include the header file and Fakskod-codes.cpp, or add these files to g++ command. Didn't help. Error raises even if I move assignments to header.

Upvotes: 0

Views: 743

Answers (1)

aschepler
aschepler

Reputation: 72463

The static class members you have declared are really named Fakskod::codesTerminateWhite and so on. When you define them in the source file, you're no longer within the class scope, so you need to type the full name:

const unsigned char Fakskod::codesTerminateBlack[64][3] = {
    // ...
};

Without that, C++ thinks they are two different arrays.

Upvotes: 2

Related Questions