0

I have two classes Node and Wire. I am getting an error for the line vector<Wire*> inputs;

Node.h

#ifndef NODE_H_
#define NODE_H_
#include "wire.h"

class Node{
private:
    bool sorted;                
    TGate gateType;             
    string name;                
    vector<Wire*> inputs;       
    vector<Wire*> outputs;      
    int state;  
    }
#endif /* NODE_H_ */

Wire.h

#ifndef WIRE_H_
#define WIRE_H_
#include "Node.h"
class Node;

class Wire{
private:
    Node* input;
    Node* output;

public:
    Wire(Node* a, Node* b);
    //void setInput(Node* in);
    //void setOutput(Node* out);
    Node* getInput();
    Node* getOutput();

};
#endif /* WIRE_H_ */

wire.cpp

#include "wire.h"
#include"node.h"

class Node;

Wire::Wire(Node* a, Node* b)
{

}

node.cpp

Node::Node(TGate gT, string name)
{
    std::cout<<"\nNode created is: "<<name<<"\n";
}

ERROR: /src/node.h:29:9: error: ‘Wire’ was not declared in this scope

user2756695
  • 676
  • 1
  • 7
  • 22
  • 2
    Circular include dependency. That can't work. Use forward declarations. – juanchopanza Apr 22 '14 at 19:32
  • @juanchopanza: Thanks, but this problem came only after i add `wire.cpp`...before that it was working well....Although by following the Answer you suggested, the problem has gone...but could you please tell me why the problem came only after i add `wire.cpp` and not before that? – user2756695 Apr 22 '14 at 19:42
  • If it seemed to work before it is by chance. Probably the lack of include guards? – juanchopanza Apr 22 '14 at 19:43
  • @juanchopanza: include guards were there...i just didn't paste them in the question at SO – user2756695 Apr 22 '14 at 19:49
  • Before you added `wire.cpp`, did you have any `.cpp` file? Compilers do not in general compile `.h` files by themselves. Also, `node.cpp` should have an `#include Node.h` so the compiler knows what a `Node` is when it compiles. – David Thornley Apr 22 '14 at 19:49
  • @DavidThornley: Yes, `node.cpp` was there. Except `wire.cpp`, everything was there – user2756695 Apr 22 '14 at 19:51
  • @DavidThornley: and actually `Class Node;` was also present in `wire.h` – user2756695 Apr 22 '14 at 19:53

1 Answers1

1

In the headers, replace

#include "Node.h"

with

class Node;

and the same for wire.

You have mutually including #includes, so "wire.h" has to include "Node.h" which has to include "wire.h", which has to include.... You need to break this chain, and to do that you use forward declarations.

The compiler needs to know that Node and Wire are classes. Since the #include files only refer to pointers to the other class, the compiler doesn't need to know the class layouts. This removes the mutual dependency and means the compiler can read all the code.

You also should have include guards, to prevent your headers from being compiled twice and causing redefinitions. Some compilers allow #pragma once, and all can handle something like

#ifndef MY_WIRE_H
#define MY_WIRE_H
...
#endif

One stylistic note: you've got Node and Wire, but "Node.h" and "wire.h". It will be easier to keep track of your files if they're consistently capitalized.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
  • actually Class Node; was already present in wire.h....now my problem got solved after i wrote `Class Wire` in `node.h`...but why do i need to write in both the files? – user2756695 Apr 22 '14 at 19:54