c++ - friend 类需要包含或转发声明 C++?

标签 c++ include forward-declaration

在尝试使用队列构建二叉树时,我一直在为错误而苦苦挣扎。问题是哪些类应该包含哪些文件以及如何从其他类中引用对象?我将文件放入 IDE 中,试图查明问题所在,结果如下。目前我的问题是在 Queue.h 文件中,treePtr“没有命名类型”。可以看到这个问题的演变here这道题和其他帖子不一样,因为这两个类是 friend 类。这就带来了循环依赖的问题。我尝试了包含文件和转发声明的各种组合,但一种组合会导致一种类型的问题,而另一种会产生不同的错误。

这是主类:

#include <cstdlib>
#include "Tree.cpp"

using namespace std;

int main() {

    Tree tree;
    tree.addTreeNode(5);

return 0;

}

这是 Queue.h:

#ifndef QUEUE_H_
#define QUEUE_H_

class Tree;  //Was instructed to put this here

class Queue {

    friend class Tree;

    private:
        typedef struct node {
            Tree::treePtr treeNode; //Here is the problem
            node* next;
        }* nodePtr;

        nodePtr head;
        nodePtr current;

public:
    Queue();
    virtual ~Queue();
    void push(Tree::treePtr t);  //Here is the problem
    int pop();
    void print();

};

#endif /* QUEUE_H_ */

这是 Tree.h:

#ifndef TREE_H_
#define TREE_H_

#include "Queue.h"  //Was instructed to put this here

class Tree {
    friend class Queue;

    private:

        Queue q;  //Edit: Most likely problem since Queue and Tree are friends

        typedef struct tree {
            int data;
            tree* left;
            tree* right;
        }* treePtr;

        treePtr root;
        int numNodes;

public:
    Tree();
    virtual ~Tree();
    void addTreeNode(int integer);
};

#endif /* TREE_H_ */

这是tree.cpp

#include <cstdlib>
#include <iostream>

#include "Tree.h"

using namespace std;

Tree::Tree() {
    root = NULL;
    numNodes = 0;
}

void Tree::addTreeNode(int integer) {
    numNodes++;
    treePtr t = new tree;
    t->left = NULL;
    t->right = NULL;
    t->data = integer;

    cout << "add root\n";
    root = t;
    q.push(t);  //This is a problem
    q.print();

}

Tree::~Tree() {
    // TODO Auto-generated destructor stub
}

最佳答案

你必须编译Tree.cpp和(我想你有一个)Queue.cpp分开,而不是包括 Tree.cpp在你的main.cpp .

前向声明适用于友好类,即使你这样做是循环的。

#include "Tree.h"在你的Queue.cpp文件,让编译器看到完整的声明。

main.cpp#include " Tree.h" .

要获得所有生成的目标文件的最终可执行链接 main.o(bj) , Tree.o(bj)Queue.o(bj) .

另见 [Why should I not include cpp files and instead use a header?]请。


正如我现在注意到的那样,您的实际问题是,您无法按照访问 treePtr 的要求从前向声明的类/结构访问嵌套类/结构。来自 Queue (treePtr 应该更好地命名为 TreeNode 或类似的 BTW)。

你不能制作treePtr在这种情况下是私有(private)嵌套类型,它必须是公开可见的。

一个可行的方法是把treePtrnamespace internal_ ,这表明它不适用于 API 之外的用途。


另一种可行的方法是制作Queue一个模板类,接受任何类型的 tree或其他类型的节点。既然看不到任何用例,为什么Queue需要了解 tree 的内部规范(除了像复制aso这样的琐碎事情。),制作Queue并不是真正必要的。一个friend类。

关于c++ - friend 类需要包含或转发声明 C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656333/

相关文章:

C++ 从另一个类访问私有(private)结构

c++ - 找出一个数的所有质因数

php - 首先为所有页面自动加载 config.php 文件

c++ - 前向声明和仿函数

c++ - 使用STL从字符串中删除重复字符

c++ - Linux 中使用三元运算符从 const char * 到 char * 的无效转换

qt - 包含来自单独目录的文件时的 undefined reference

perl - 无法在@INC 中找到 XML/XPath.pm

c++ - 将类前向声明​​为模板参数

c++ - 如何访问单独类中的私有(private)构造函数?