简体   繁体   English

g ++抛出“未定义的引用”静态类成员

[英]g++ throws “undefined reference to” static class member

I'm writing CCITT Group 4 encoder and it seems I have a problem with linker. 我正在编写CCITT Group 4编码器,看起来我的链接器有问题。

I have some raw binary data stored as static const class members ( Fakskod.h ): 我有一些原始二进制数据存储为static const类成员( 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: 所以在另一个文件( Fakskod-codes.cpp )中我分配了数据:

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 ): 似乎麻烦的来源是这种方法( 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. 预处理器( g++ -E )正确解析它,当我将扩展版本粘贴到我的代码时,消息是相同的。

Also I tried to #include the header file and Fakskod-codes.cpp , or add these files to g++ command. 我还尝试#include头文件和Fakskod-codes.cpp ,或者将这些文件添加到g ++命令。 Didn't help. 没有帮助。 Error raises even if I move assignments to header. 即使我将分配移动到标头,错误也会增加。

The static class members you have declared are really named Fakskod::codesTerminateWhite and so on. 您声明的static类成员实际上名为Fakskod::codesTerminateWhite ,依此类推。 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. 没有它,C ++认为它们是两个不同的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM