2

My question is about the inclusion of a typedef struct (or at least I think it is). 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:

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.

Then I have a Graph.h header, including the Node.h file, that declares a Graph class.

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:

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.

As you probably can see I'm kind of a C++ newbie. 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. Am I wrong? Or didn't I understand how includes work in C++?

I hope I didn't miss any detail. Thank you so much for the help!

jackscorrow
  • 682
  • 1
  • 9
  • 27
  • 2
    `Node::EdgeInfo`. But why do you use a `typedef` at that point at all? They're not necessary in C++ (in that context usually). – Zeta Jan 06 '17 at 09:11
  • @Zeta Why? What would you do instead? – jackscorrow Jan 06 '17 at 12:05
  • 1
    @Tommaso Scarpa In c++ you don't need to use the keyword `typedef` to declare a `struct`, or a `class`. It's valid but not necessary. You can just declare your `struct` by doing : `struct S { int a; };` And you declare a instance of `struct S` by doing : `S s; s.a = 10` – nikau6 Jan 06 '17 at 12:29
  • @nikau6 I tried but if I remove `typedef` my code doesn't compile – jackscorrow Jan 06 '17 at 13:09
  • @Tommaso Scarpa You must declare your `struct` like that : `struct S {};`, if you do what I think you do : `struct {}S;` it'll not compile. – nikau6 Jan 06 '17 at 13:12
  • @nikau6 Oh, you're right! Thank you! – jackscorrow Jan 06 '17 at 13:15

1 Answers1

2

"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."

You could use a forward declaration. A another link: forward declaration, 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.

nikau6
  • 922
  • 9
  • 16