1

好的,这是基本的 C++。我有一个类是线性链接列表。我想声明两个结构。其中一个是供客户端使用的(即他们将传递给类的信息),另一个结构仅供我,实施者管理链表(这是“节点”)。IE:

//this is the one for client use
struct form {
    char *name;
    int age;
    char *address;
    //etc.
};

struct node {
    char *name; //same as in above but I am sorting the LL by this so I need it out
    form *client_form;
    node *next;
};

我感到困惑的是究竟在哪里放置这些。我认为最好将客户端将使用的结构放在类定义之上,但是放置“节点”结构的最佳位置是哪里。这应该私下吗?多谢你们!

4

1 回答 1

3

节点结构可以简单地放入您的 .cpp 文件中。如果您的标头中有一些引用节点的内容,例如“struct node *firstNode”,那么您必须在标头顶部附近转发声明它,只需说“struct node;”即可。

所以,.h:

struct node;
struct form {
   // form fields
};

class MyStuff {
   private:
      struct node *firstNode;
   // more Stuff
};

.cpp:

struct node {
   // node fields
};

MyStuff::MyStuff(struct form const& details) {
    // code
}
于 2012-04-18T19:11:13.953 回答