C ++循环依赖混淆与邻接表的表示 [英] C++ cyclic dependency confusion with adjacency list representation

查看:175
本文介绍了C ++循环依赖混淆与邻接表的表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,因为我没有经验C ++,但我花了一段时间安静一段时间解决循环依赖问题,因此构成这个。

Sorry for my inexperience with C++, but I spent quiet some time with solving a cyclic dependency issue and hence posing this.

我试图代表邻接列表在C ++中。

I am trying to represent a Adjacency List in C++.

我有struct Node

struct Node{

    int data;
    unordered_set<Node, Hash> links;

    bool operator == (Node const& other) const{
        return (data == other.data);
    }

    Node(){
    }

    Node(int data){
        this->data = data;
    }
};

我有我的 Hash p>

and I have my Hash functor

struct Hash {
    size_t operator()(const Node &node) const {
        return node.data;
    };
};

我注意到 Hash 使用 Node 节点使用 Hash

这个练习的目的我想在一个文件中声明一切,我应该首先声明。

I noticed that Hash uses Node and Node uses Hash
If for the purpose of this exercise I want to declare everything in a single file, which one should I declare first.

我试着向前声明 Hash 节点,首先定义它们,但没有编译。

I tried forward declaration of both Hash and Node with defining either of them first, but none of them compiled.

PS:这不是家庭作业,我想在线解决图形算法谜题

PS: This is not homework, I'm trying to solve graph algorithm puzzles online

推荐答案

延迟定义 Hash :: operator(),直到定义节点,并声明节点之前 Hash

Delay defining Hash::operator() until after defining Node and declare Node before Hash. You can have a reference to an incomplete type as long as you don't do anything with it.

class Node;

class Hash{
    public:
        size_t operator()(const Node &node) const;
};

class Node{
    public:
        int data;
        unordered_set<Node, Hash> links;
};

inline size_t Hash::operator()(const Node &node) const{
    return node.data;
}

这篇关于C ++循环依赖混淆与邻接表的表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆