gpt4 book ai didi

c++ - 交叉引用类对象

转载 作者:行者123 更新时间:2023-11-30 00:37:12 24 4
gpt4 key购买 nike

我做了这样的事情:

struct Vertex {
list<Edge*> edges;
};

struct Edge {
Vertex* v1;
Vertex* v2;
};

编译器错误:

'Edge' was not declared in this scope

如何在不将这两个文件放入单独的 header “vertex.h”和“edge.h”的情况下解决这个问题?

最佳答案

在使用 Edge 之前使用前向声明

struct Edge;

struct Vertex
{
list<Edge*> edges;
};

请注意,当您前向声明一个类型时,编译器将该类型视为不完整类型,它不知道该类型的布局,但它只知道该类型存在,因此对于如何使用不完整类型存在一些限制。

好读:
When can I use a forward declaration?

关于c++ - 交叉引用类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13814076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com