簡體   English   中英

在 C++ 中訪問聯合內的結構時出現問題

[英]Issue in accessing a structure inside a union in C++

   union hello
   {
      struct hi
      {
         uint8_t trees;
         uint8_t plants[6];
      };
      uint8_t forest[1 + 6];
   };

   struct world
   {
      hello a;
   };

typedef std::vector<world> universe;
universe alpha;

如果我想訪問 hello 里面的結構 hi ,那么我正在做類似的事情,假設訪問第一個向量元素

alpha.at(1).a.hi.trees = 1; // error in accessing "hi" , why can't I access hi like this?  

錯誤:不允許使用類型名稱

在您的版本中,該struct不是聯合字段; 它只是嵌套類型名稱。 將其更改為:

   union hello
   {
      struct
      {
         uint8_t trees;
         uint8_t plants[6];
      } hi;/*<< put the field name here*/
      uint8_t forest[1 + 6];
   };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM