【问题标题】:can two structs defined in same class can access each other?同一个类中定义的两个结构可以互相访问吗?
【发布时间】:2013-09-28 19:07:10
【问题描述】:

如果我有一门课本有两个 struct

struct A{
    B *ptr;   //it says identifier undefined 
};
struct B{

};

两者都定义在同一个类中。如上所述,是否可以将结构 B 的指针保存在结构 A 中? 有人可以帮忙吗?

【问题讨论】:

  • struct B放在struct A之前。
  • 非常感谢 =) 为我工作
  • alot.
  • @jrok oooppps =p 非常感谢您的纠正

标签: c++ class pointers struct


【解决方案1】:

在 C++ 中,所有符号都必须在使用前声明。所以只需将B 结构放在A 结构之前。

【讨论】:

    【解决方案2】:

    你只需要先声明struct B

    struct B;
    struct A{
        B *ptr;   //it says identifier undefined 
    };
    struct B{
    
    };
    

    【讨论】:

      【解决方案3】:

      struct B;
      

      首先,您就完成了。这告诉编译器会有一个B

      【讨论】:

        【解决方案4】:

        是的,你可以,你只需要declare the structure name before你使用实例的结构

        struct B; // declared before struct A, now the problem is gone.
        
        struct A{
            B *ptr;   //it says identifier undefined 
        };
        
        
        struct B{
        
        };
        

        【讨论】:

          【解决方案5】:

          将支柱 B 放在结构 A 之前。

           class book
          {
          struct B
          {
          
          };
          struct A
          {
           B * ptr;         
          };
          
          };
          

          将结构 B 放在结构 A 之前。

          【讨论】:

            猜你喜欢
            • 2013-04-19
            • 1970-01-01
            • 2013-01-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-09-12
            • 2020-03-07
            • 1970-01-01
            相关资源
            最近更新 更多