<legend id='wSh2k'><style id='wSh2k'><dir id='wSh2k'><q id='wSh2k'></q></dir></style></legend>
  • <small id='wSh2k'></small><noframes id='wSh2k'>

      <bdo id='wSh2k'></bdo><ul id='wSh2k'></ul>
      <i id='wSh2k'><tr id='wSh2k'><dt id='wSh2k'><q id='wSh2k'><span id='wSh2k'><b id='wSh2k'><form id='wSh2k'><ins id='wSh2k'></ins><ul id='wSh2k'></ul><sub id='wSh2k'></sub></form><legend id='wSh2k'></legend><bdo id='wSh2k'><pre id='wSh2k'><center id='wSh2k'></center></pre></bdo></b><th id='wSh2k'></th></span></q></dt></tr></i><div id='wSh2k'><tfoot id='wSh2k'></tfoot><dl id='wSh2k'><fieldset id='wSh2k'></fieldset></dl></div>

        <tfoot id='wSh2k'></tfoot>

        “静态常量整数"导致链接错误(未定义引用)

        时间:2023-12-02
        <i id='VhT4P'><tr id='VhT4P'><dt id='VhT4P'><q id='VhT4P'><span id='VhT4P'><b id='VhT4P'><form id='VhT4P'><ins id='VhT4P'></ins><ul id='VhT4P'></ul><sub id='VhT4P'></sub></form><legend id='VhT4P'></legend><bdo id='VhT4P'><pre id='VhT4P'><center id='VhT4P'></center></pre></bdo></b><th id='VhT4P'></th></span></q></dt></tr></i><div id='VhT4P'><tfoot id='VhT4P'></tfoot><dl id='VhT4P'><fieldset id='VhT4P'></fieldset></dl></div>

          • <bdo id='VhT4P'></bdo><ul id='VhT4P'></ul>
              <tbody id='VhT4P'></tbody>

              <legend id='VhT4P'><style id='VhT4P'><dir id='VhT4P'><q id='VhT4P'></q></dir></style></legend>
                <tfoot id='VhT4P'></tfoot>

                  <small id='VhT4P'></small><noframes id='VhT4P'>

                  本文介绍了“静态常量整数"导致链接错误(未定义引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对使用以下代码时的链接器错误感到困惑:

                  I am baffled by the linker error when using the following code:

                  // static_const.cpp -- complete code
                  #include <vector>
                  
                  struct Elem {
                      static const int value = 0;
                  };
                  
                  int main(int argc, char *argv[]) {
                      std::vector<Elem> v(1);
                      std::vector<Elem>::iterator it;
                  
                      it = v.begin();
                      return it->value;
                  }
                  

                  然而,这在链接时会失败——不知何故,它需要有一个代表静态常量值"的符号.

                  However, this fails when linking -- somehow it needs to have a symbol for the static const "value."

                  $ g++ static_const.cpp 
                  /tmp/ccZTyfe7.o: In function `main':
                  static_const.cpp:(.text+0x8e): undefined reference to `Elem::value'
                  collect2: ld returned 1 exit status
                  

                  顺便说一句,这在 -O1 或更好的情况下编译得很好;但对于更复杂的情况,它仍然失败.我使用的是 gcc 版本 4.4.4 20100726 (Red Hat 4.4.4-13).

                  BTW, this compiles fine with -O1 or better; but it still fails for more complicated cases. I am using gcc version 4.4.4 20100726 (Red Hat 4.4.4-13).

                  知道我的代码可能有什么问题吗?

                  Any ideas what might be wrong with my code?

                  推荐答案

                  如果你想在 struct 内部初始化它,你也可以这样做:

                  If you want to initialize it inside the struct, you can do it too:

                  struct Elem {
                      static const int value = 0;
                  };
                  
                  const int Elem::value;
                  

                  这篇关于“静态常量整数"导致链接错误(未定义引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:跨文件共享静态变量:命名空间与类 下一篇:“未定义的引用"到从静态方法访问的模板类的静态成员

                  相关文章

                • <legend id='3IDH7'><style id='3IDH7'><dir id='3IDH7'><q id='3IDH7'></q></dir></style></legend>
                • <tfoot id='3IDH7'></tfoot>
                  <i id='3IDH7'><tr id='3IDH7'><dt id='3IDH7'><q id='3IDH7'><span id='3IDH7'><b id='3IDH7'><form id='3IDH7'><ins id='3IDH7'></ins><ul id='3IDH7'></ul><sub id='3IDH7'></sub></form><legend id='3IDH7'></legend><bdo id='3IDH7'><pre id='3IDH7'><center id='3IDH7'></center></pre></bdo></b><th id='3IDH7'></th></span></q></dt></tr></i><div id='3IDH7'><tfoot id='3IDH7'></tfoot><dl id='3IDH7'><fieldset id='3IDH7'></fieldset></dl></div>

                    • <bdo id='3IDH7'></bdo><ul id='3IDH7'></ul>

                      <small id='3IDH7'></small><noframes id='3IDH7'>