简体   繁体   English

C++ - 从类内部包含 typedef 结构

[英]C++ - inclusion of typedef struct from inside a class

My question is about the inclusion of a typedef struct (or at least I think it is).我的问题是关于包含一个 typedef 结构(或者至少我认为是)。 It's probably silly but I couldn't figure out what's wrong with my code.这可能很愚蠢,但我无法弄清楚我的代码有什么问题。

I have a Node.h header that declares a class Node :我有一个Node.h头文件,它声明了一个类Node

class Node {
public:
    Node(/*some parameters*/); // Class constructor

    typedef struct{ // This represents a weighted edge of weight "weight" from the current node to another node "node"
        Node* node;
        double weight;
    }EdgeInfo;

    vector<EdgeInfo*> OutEdges; // This vector represents the outgoing weighted edges from this node
};

So I figured out that's the only place in my code I can declare the struct because that's the only place where I can declare EdgeInfo that "knows" what a Node class object is.所以我发现这是我的代码中唯一可以声明struct的地方,因为这是我可以声明“知道” Node类对象是什么的EdgeInfo的唯一地方。

Then I have a Graph.h header, including the Node.h file, that declares a Graph class.然后我有一个Graph.h头文件,包括Node.h文件,它声明了一个Graph类。

Now in the Graph.cpp file I'm implementing a function that loops on all the outgoing edges of a Node , more or less like this:现在在Graph.cpp文件中,我正在实现一个在Node所有传出边缘上循环的函数,或多或少是这样的:

Node* current = /*passing an object of class Node*/

for(EdgeInfo* out : current->OutEdges){           
    /*working on the edges*/
}

The problem is that when I compile I get the error error: 'EdgeInfo' was not declared in this scope at the for loop.问题是,当我编译时,我收到错误error: 'EdgeInfo' was not declared in this scope for循环的error: 'EdgeInfo' was not declared in this scope

As you probably can see I'm kind of a C++ newbie.正如您可能看到的,我是一个 C++ 新手。 I thought that by including Node.h I could use the ´typedef struct` definitions inside the class the same way I use variables and functions.我认为通过包含Node.h我可以像使用变量和函数一样在类中使用“typedef struct”定义。 Am I wrong?我错了吗? Or didn't I understand how includes work in C++?或者我不明白包含在 C++ 中是如何工作的?

I hope I didn't miss any detail.我希望我没有遗漏任何细节。 Thank you so much for the help!十分感谢你的帮助!

" So I figured out that's the only place in my code I can declare the struct because that's the only place where I can declare EdgeInfo that "knows" what a Node class object is. " 所以我发现这是我的代码中唯一可以声明结构的地方,因为这是我可以声明 EdgeInfo 的唯一地方,它“知道”Node 类对象是什么。

You could use a forward declaration .您可以使用前向声明 A another link: forward declaration, stackoverflow .另一个链接: 前向声明,stackoverflow

class Node; // forward declaration

struct EdgeInfo
{ 
    Node* node; // now, `EdgeInfo` knows that a class named `Node` is defined somewhere.
    double weight;
};

class Node {
public:
    Node(/*some parameters*/); // Class constructor

    vector<EdgeInfo*> OutEdges; // This vector represents the outgoing weighted edges from this node
};

Now when you include node.h in graph.cpp , or Graph.h , it'll be aware about EdgeInfo .现在,当您在graph.cppGraph.h包含node.h时,它会知道EdgeInfo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM