【发布时间】:2014-04-23 10:44:09
【问题描述】:
注意:我发现了问题。在Wire.cpp,我使用了
Wire::Wire(Node* a, Node* b)
{
}
应该是
Wire::Wire(Node* a, Node* b) : input(a),output(b)
{
}
我原来的问题如下:
我有两个课程Node.h 和Wire.h。当我调用class Wire 的getInput() 方法时,它应该返回一个指针,但它只是给出一个整数值。
问题:在void Node::eval()中,我打电话给input[0]->getInput()->getState()。我使用Node* Wire::getInput()中的打印语句进行调试,发现程序进入了那个方法。但是该方法应该返回一个有效的指针,以便我可以调用下一个方法void Node::getState()。但后来我得到 segmentation fault
Wire.h
class Node;
class Wire{
private:
Node* input;
Node* output;
public:
Wire(Node* a, Node* b);
Node* getInput();
Node* getOutput();
};
wire.cpp
Wire::Wire(Node* a, Node* b)
{
}
Node* Wire::getInput(){
cout<<"\nInput: "<<input;
return input;
}
Node* Wire::getOutput(){
return output;
}
Node.h
typedef enum {
UNDEFINED, INPUT, OUTPUT, AND, NAND, OR, NOR, NOT, XOR
} TGate;
class Node{
private:
TGate gateType; //Type of the Node
string name; //Name of the gate (the name of the output in .bench file)
vector<Wire*> inputs;
vector<Wire*> outputs;
int state;
public:
void addOutput(Wire *a);
void addInput(Wire *a);
Node* getInput(unsigned int i);
Node* getOutput(unsigned int i);
void setState(int st);
int getState(void);
};
node.cpp
void Node::addInput(Wire *a)
{
inputs.push_back(a);
}
void Node::addOutput(Wire *a)
{
outputs.push_back(a);
}
string Node::getName()
{
return name;
}
void Node::setState(int st)
{
state = st;
cout<<"\nState set to: "<<state;
}
int Node::getState(void)
{
//return 0;
return state;
}
void Node::eval()
{
if(inputs[0]->getInput()->getState() == 1)
cout<<"Node is in rest state."
}
main()
int main(int argc, char *argv[])
{
Node* b=new Node(INPUT, "B");
Node* a=new Node(INPUT, "A");
Node* Cin=new Node(INPUT, "Cin");
Node* d=new Node(XOR, "D");
Wire* w=new Wire(a,d);
d->addInput(w);
a->addOutput(w);
w=new Wire(b,d);
d->addInput(w);
b->addOutput(w);
Node* e=new Node(AND, "E");
w=new Wire(d,e);
e->addInput(w);
d->addOutput(w);
w=new Wire(Cin,e);
e->addInput(w);
Cin->addOutput(w);
Node* f=new Node(AND, "F");
w=new Wire(a,f);
f->addInput(w);
a->addOutput(w);
w=new Wire(b,f);
f->addInput(w);
b->addOutput(w);
Node* s=new Node(XOR, "S");
w=new Wire(d,s);
s->addInput(w);
d->addOutput(w);
w=new Wire(Cin,s);
s->addInput(w);
Cin->addOutput(w);
Node* Cout=new Node(OR, "Cout");
w=new Wire(e,Cout);
Cout->addInput(w);
e->addOutput(w);
w=new Wire(f,Cout);
Cout->addInput(w);
f->addOutput(w);
Node* out_s=new Node(OUTPUT, "S");
w=new Wire(s,out_s);
out_s->addInput(w);
s->addOutput(w);
Node* out_Cout=new Node(OUTPUT,"Cout");
w=new Wire(Cout,out_Cout);
out_Cout->addInput(w);
Cout->addOutput(w);
vector<Node*> inputs;
vector<Node*> gates;
vector<Node*> outputs;
inputs.push_back(a);
inputs.push_back(b);
inputs.push_back(Cin);
gates.push_back(d);
gates.push_back(e);
gates.push_back(f);
gates.push_back(Cout);
gates.push_back(s);
outputs.push_back(out_s);
outputs.push_back(out_Cout);
//simulate circuit for 5 random inputs
for(int i=0;i<5;i++)
{
for(unsigned int j=0;j<inputs.size();j++)
{
inputs[j]->setState(rand()%2);
cout << inputs[j]->getState();
}
cout <<" - ";
for(unsigned int j=0;j<gates.size();j++)
{
gates[j]->eval();
}
}
return 0;
【问题讨论】:
-
要粘贴完整的程序吗?
-
@tenfour:我已经发布了失败的功能。
getInput()应该返回一个指针,但它不是。 -
您粘贴的代码没有问题。问题出在其他地方,这就是人们可能希望看到完整程序的原因。
-
@user2756695 我假设
input属性正在使用a值初始化。如果是这种情况,请检查a的实际参数是否为有效地址。 -
我想你确定
inputs向量不为空?无论如何,最好在Node::eval()中添加一个检查:if( !inputs.empty() ){your_code}请告诉我们你如何创建连线和节点。
标签: c++