• <small id='SQMPi'></small><noframes id='SQMPi'>

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

      1. <tfoot id='SQMPi'></tfoot>

        对静态变量 c++ 的未定义引用

        Undefined reference to static variable c++(对静态变量 c++ 的未定义引用)
      2. <i id='TI7YI'><tr id='TI7YI'><dt id='TI7YI'><q id='TI7YI'><span id='TI7YI'><b id='TI7YI'><form id='TI7YI'><ins id='TI7YI'></ins><ul id='TI7YI'></ul><sub id='TI7YI'></sub></form><legend id='TI7YI'></legend><bdo id='TI7YI'><pre id='TI7YI'><center id='TI7YI'></center></pre></bdo></b><th id='TI7YI'></th></span></q></dt></tr></i><div id='TI7YI'><tfoot id='TI7YI'></tfoot><dl id='TI7YI'><fieldset id='TI7YI'></fieldset></dl></div>
        • <bdo id='TI7YI'></bdo><ul id='TI7YI'></ul>

          <tfoot id='TI7YI'></tfoot>

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

                <legend id='TI7YI'><style id='TI7YI'><dir id='TI7YI'><q id='TI7YI'></q></dir></style></legend>
                    <tbody id='TI7YI'></tbody>
                  本文介绍了对静态变量 c++ 的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在以下代码中遇到未定义的引用错误:

                  Hi i am getting undefined reference error in the following code:

                  class Helloworld{
                    public:
                       static int x;
                       void foo();
                  };
                  void Helloworld::foo(){
                       Helloworld::x = 10;
                  };
                  

                  我不想要 static foo() 函数.如何在类的非 static 方法中访问类的 static 变量?

                  I don't want a static foo() function. How can I access static variable of a class in non-static method of a class?

                  推荐答案

                  我不想要一个 static foo() 函数

                  好吧,foo() 在你的类中不是静态的,你不需要让它staticcode> 以访问您的类的 static 变量.

                  Well, foo() is not static in your class, and you do not need to make it static in order to access static variables of your class.

                  您需要做的只是为您的静态成员变量提供一个定义:

                  What you need to do is simply to provide a definition for your static member variable:

                  class Helloworld {
                    public:
                       static int x;
                       void foo();
                  };
                  
                  int Helloworld::x = 0; // Or whatever is the most appropriate value
                                         // for initializing x. Notice, that the
                                         // initializer is not required: if absent,
                                         // x will be zero-initialized.
                  
                  void Helloworld::foo() {
                       Helloworld::x = 10;
                  };
                  

                  这篇关于对静态变量 c++ 的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                    <bdo id='JmzGr'></bdo><ul id='JmzGr'></ul>
                    <legend id='JmzGr'><style id='JmzGr'><dir id='JmzGr'><q id='JmzGr'></q></dir></style></legend>
                      <i id='JmzGr'><tr id='JmzGr'><dt id='JmzGr'><q id='JmzGr'><span id='JmzGr'><b id='JmzGr'><form id='JmzGr'><ins id='JmzGr'></ins><ul id='JmzGr'></ul><sub id='JmzGr'></sub></form><legend id='JmzGr'></legend><bdo id='JmzGr'><pre id='JmzGr'><center id='JmzGr'></center></pre></bdo></b><th id='JmzGr'></th></span></q></dt></tr></i><div id='JmzGr'><tfoot id='JmzGr'></tfoot><dl id='JmzGr'><fieldset id='JmzGr'></fieldset></dl></div>

                        <tfoot id='JmzGr'></tfoot>

                            <tbody id='JmzGr'></tbody>
                        • <small id='JmzGr'></small><noframes id='JmzGr'>