方法不返回有效指针

时间:2014-04-23 10:44:09

标签: c++

注意:我发现了问题。在Wire.cpp中,我使用了

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

}

应该是

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

}

我原来的问题如下:

我有两个班级Node.hWire.h。当我调用getInput()的方法class Wire时,它应该返回一个指针,但它只是给出一个整数值。

问题: void Node::eval() 中,我正在呼叫input[0]->getInput()->getState()。我在Node* Wire::getInput()中使用print语句调试,发现程序进入该方法。但是该方法应该返回一个有效的指针,以便我可以调用下一个方法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;

2 个答案:

答案 0 :(得分:1)

Pointers是标识内存映射中特定字节的整数值。

我认为你真正想要的是按照一种格式打印的指针,表明该值是一个地址。例如,0xHEX_VAL

就像这个问题一样:How to simulate printf's %p format when using std::cout?

修改

更新代码后,我发现您没有初始化input output个属性。

尝试使用Wire类的whis构造函数:

Wire::Wire(Node* a, Node* b)
{
    input = a;
    output = b;
}

答案 1 :(得分:0)

转换为void *,您应该以十六进制显示输出。这就是你所需要的。

cout<<"\n Input address: "<<(void *)(inputs[0]->getInput());