我有两个班级Node
和Wire
。我收到了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";
}
错误: /src/node.h:29:9:错误:未在此范围内声明'有线'
答案 0 :(得分:1)
在标题中,替换
#include "Node.h"
与
class Node;
和wire
相同。
您必须包含#include
s,因此"wire.h"
必须包含"Node.h"
,其中必须包含"wire.h"
,其中必须包含....您需要打破这个链,为此你使用前向声明。
编译器需要知道Node
和Wire
是类。由于#include
文件只引用指向另一个类的指针,因此编译器不需要知道类布局。这消除了相互依赖性,意味着编译器可以读取所有代码。
您还应该有包含警戒,以防止您的标头被编译两次并导致重新定义。有些编译器允许#pragma once
,并且所有编译器都可以处理类似
#ifndef MY_WIRE_H
#define MY_WIRE_H
...
#endif
一个风格说明:您有Node
和Wire
,但"Node.h"
和"wire.h"
。如果文件始终大写,那么跟踪文件会更容易。