Xcode 不链接两个结构

Xcode not linking two structures?

本文关键字:两个 结构 链接 Xcode      更新时间:2023-10-16

为什么Xcode说"Vertex.h"无法识别?

//Vertex.h
#include "Edge.h"
struct Vertex 
{
     vector<Edge> adjList;
     string myData;
};

在单独的文件中,

//Edge.h
#include "Vertex.h"
struct Edge
{
    Vertex* destination; // "Unknown type named 'Vertex.h'
    double weight;
}

这里有错误吗?请帮忙。谢谢!

你有一个循环依赖:Vertex.h取决于Edge.h取决于Vertex.h......

在您的特定情况下,这很容易解决,因为Edge.h不需要Vertex的完整定义,只知道Vertex结构存在,因此请像更改它一样

//Edge.h
struct Vertex;
struct Edge
{
    Vertex* destination; // "Unknown type named 'Vertex.h'
    double weight;
};

更改是不包含Vertex.h,而是声明Vertex结构。这告诉编译器存在一个名为Vertex的结构,因此它可以用于例如指针或引用。