【问题标题】:method is not returing a valid pointer方法没有返回有效的指针
【发布时间】: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.hWire.h。当我调用class WiregetInput() 方法时,它应该返回一个指针,但它只是给出一个整数值。

问题: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++


【解决方案1】:

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

我认为您真正想要的是按照表明值是地址的格式打印的指针。例如0xHEX_VAL

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

编辑:

更新代码后,我可以看到您没有初始化 inputoutput 属性。

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

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

【讨论】:

  • 请查看我更新的问题....问题不仅仅是打印地址。 getInput() 应该返回一个有效的指针,以便我可以调用下一个方法(即getState()
  • @user2756695 为了帮助您解决这个问题,我们需要更多关于如何初始化 input 属性的信息。
  • 我更新了node.cpp 的代码,其中还包含getState();
  • 是的,这解决了问题,但你能告诉我为什么我需要这样做吗?
  • @user2756695 因为否则,您不会在程序中告诉input 获取a 值,也不会output 获取b 值。您必须在您的 Wire 对象中分配 input 一个 output 值,如何?通过在类构造函数中分配它们,使用哪些值?您作为构造函数参数传递的那些。
【解决方案2】:

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

cout&lt;&lt;"\n Input address: "&lt;&lt;(void *)(inputs[0]-&gt;getInput());

【讨论】:

  • 请查看我更新的问题....问题不仅仅是打印地址。 getInput() 应该返回一个有效的指针,以便我可以调用下一个方法(即getState()
猜你喜欢
  • 2017-11-29
  • 2016-02-25
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 1970-01-01
相关资源
最近更新 更多