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

      • <bdo id='z6OwL'></bdo><ul id='z6OwL'></ul>

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

        <tfoot id='z6OwL'></tfoot>

        不能在全局范围内使用结构

        can#39;t use structure in global scope(不能在全局范围内使用结构)

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

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

                  本文介绍了不能在全局范围内使用结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在全局范围内定义了 struct,但是当我尝试使用它时,我得到错误:'co' 没有命名类型,但是当我在函数中执行相同操作时,一切工作正常

                  I defined struct in the global scope, but when I try to use it, I get error: ‘co’ does not name a type, but when I do the same in a function, everything works fine

                  typedef struct {
                    int x;
                    int y;
                    char t;
                  } MyStruct;
                  
                    MyStruct co;
                    co.x = 1;
                    co.y = 2;
                    co.t = 'a'; //compile error
                  
                  void f() {
                    MyStruct co;
                    co.x = 1;
                    co.y = 2;
                    co.t = 'a';
                    cout << co.x << '	' << co.y << '	' << co.t << endl;
                  } //everything appears to work fine, no compile errors
                  

                  我做错了什么,还是结构不能在全局范围内使用?

                  Am I doing something wrong, or structures just cannot be used in global scope?

                  推荐答案

                  并不是说您不能在全局范围内使用结构".这里的结构没有什么特别之处.

                  It's not that you "can't use structures in global scope". There is nothing special here about structures.

                  您根本无法编写程序代码,例如函数体之外的赋值.任何对象就是这种情况:

                  You simply cannot write procedural code such as assignments outside of a function body. This is the case with any object:

                  int x = 0;
                  x = 5; // ERROR!
                  
                  int main() {}
                  

                  此外,向后 typedef 是上个世纪的废话(在 C++ 中不需要).

                  Also, that backwards typedef nonsense is so last century (and not required in C++).

                  如果您要初始化对象,请执行以下操作:

                  If you're trying to initialise your object, do this:

                  #include <iostream>
                  
                  struct MyStruct
                  {
                     int x;
                     int y;
                     char t;
                  };
                  
                  MyStruct co = { 1, 2, 'a' };
                  
                  int main()
                  {
                     std::cout << co.x << '	' << co.y << '	' << co.t << std::endl;
                  }
                  

                  这篇关于不能在全局范围内使用结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                  Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)

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

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

                          <tfoot id='AqjNI'></tfoot>
                          <legend id='AqjNI'><style id='AqjNI'><dir id='AqjNI'><q id='AqjNI'></q></dir></style></legend>
                            <tbody id='AqjNI'></tbody>

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