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

    <bdo id='aw14O'></bdo><ul id='aw14O'></ul>

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

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

      2. <legend id='aw14O'><style id='aw14O'><dir id='aw14O'><q id='aw14O'></q></dir></style></legend>

        从另一个文件访问 C++ 中的外部变量

        Access extern variable in C++ from another file(从另一个文件访问 C++ 中的外部变量)
        • <bdo id='oY3lO'></bdo><ul id='oY3lO'></ul>

            <tbody id='oY3lO'></tbody>
            <legend id='oY3lO'><style id='oY3lO'><dir id='oY3lO'><q id='oY3lO'></q></dir></style></legend>

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

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

                1. 本文介绍了从另一个文件访问 C++ 中的外部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在其中一个 cpp 文件中有一个全局变量,我正在为其分配一个值.现在为了能够在另一个 cpp 文件中使用它,我将它声明为 extern 并且这个文件有多个使用它的函数,所以我在全局执行此操作.现在可以在其中一个函数中访问此变量的值,而不能在另一个函数中访问.除了在头文件中使用它之外的任何建议都会很好,因为我浪费了 4 天的时间.

                  I have a global variable in one of the cpp files, where I am assigning a value to it. Now in order to be able to use it in another cpp file, I am declaring it as extern and this file has multiple functions that use it so I am doing this globally. Now the value of this variable can be accessed in one of the functions and not in the other one. Any suggestions except using it in a header file would be good because I wasted 4 days playing with that.

                  推荐答案

                  抱歉,我忽略了对建议使用头文件以外的任何其他内容的答案的请求.这就是标题的用途,当您正确使用它们时...仔细阅读:

                  Sorry, I'm ignoring the request for answers suggesting anything other than the use of header files. This is what headers are for, when you use them correctly... Read carefully:

                  global.h

                  #ifndef MY_GLOBALS_H
                  #define MY_GLOBALS_H
                  
                  // This is a declaration of your variable, which tells the linker this value
                  // is found elsewhere.  Anyone who wishes to use it must include global.h,
                  // either directly or indirectly.
                  extern int myglobalint;
                  
                  #endif
                  

                  global.cpp

                  #include "global.h"
                  
                  // This is the definition of your variable.  It can only happen in one place.
                  // You must include global.h so that the compiler matches it to the correct
                  // one, and doesn't implicitly convert it to static.
                  int myglobalint = 0;
                  

                  user.cpp

                  // Anyone who uses the global value must include the appropriate header.
                  #include "global.h"
                  
                  void SomeFunction()
                  {
                      // Now you can access the variable.
                      int temp = myglobalint;
                  }
                  

                  现在,当您编译和链接您的项目时,您必须:

                  Now, when you compile and link your project, you must:

                  1. 将每个源 (.cpp) 文件编译为目标文件;
                  2. 链接所有目标文件以创建您的可执行文件/库/任何内容.

                  使用我上面给出的语法,您应该不会有编译和链接错误.

                  Using the syntax I have given above, you should have neither compile nor link errors.

                  这篇关于从另一个文件访问 C++ 中的外部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)

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

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

                        <legend id='djc7Y'><style id='djc7Y'><dir id='djc7Y'><q id='djc7Y'></q></dir></style></legend>
                          <bdo id='djc7Y'></bdo><ul id='djc7Y'></ul>