首页 > 解决方案 > 如何访问在 C++ 类中声明的结构类型指针变量?

问题描述

class btree{
 public:
 int non,vor;
 string str;
 struct tree{
   int data;
   struct tree *lnode;
   struct tree *rnode;
    };
};
int main(){
}

如何在 main 中访问结构类型指针 lnode 甚至可能有帮助吗????

标签: c++classpointersdata-structuresstructure

解决方案


您定义了struct tree,但实际上并没有将任何内容放入您的btree. 改为这样做:

class btree {
public:
    int non, vor;
    string str;
    struct tree {
        int data;
        struct tree *lnode;
        struct tree *rnode;
    } node; // note the "node" that was added, now btree contains a struct tree named "node"
};

像这样访问它:

int main() {
    btree myTree;
    myTree.node.data = 10;
}

推荐阅读