C++ 方法未重试有效指针

C++ 方法未重试有效指针,c++,C++,注意:我发现了问题。在Wire.cpp中,我使用了 Wire::Wire(Node* a, Node* b) { } 应该是哪一个 Wire::Wire(Node* a, Node* b) : input(a),output(b) { } 我的原始问题如下: 我有两个类Node.h和Wire.h。当我调用类Wire的方法getInput()时,它应该返回一个指针,但它只给出一个整数值 问题:在void Node::eval()中,我正在调用input[0]->getInput()->ge

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

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

}
应该是哪一个

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

}
我的原始问题如下:

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

问题:
void Node::eval()
中,我正在调用
input[0]->getInput()->getState()。我在
节点*Wire::getInput()
中使用print语句进行调试,发现程序进入了该方法。但该方法应该返回一个有效指针,这样我就可以调用下一个方法
void Node::getState()
。但是我得到了
分段错误

class Node;

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

public:
    Wire(Node* a, Node* b);
    Node* getInput();
    Node* getOutput();

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

}

Node* Wire::getInput(){
    cout<<"\nInput: "<<input;
    return input;
}

Node* Wire::getOutput(){
    return output;
}
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);            


};
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."
}   
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;
Wire.h

class Node;

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

public:
    Wire(Node* a, Node* b);
    Node* getInput();
    Node* getOutput();

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

}

Node* Wire::getInput(){
    cout<<"\nInput: "<<input;
    return input;
}

Node* Wire::getOutput(){
    return output;
}
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);            


};
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."
}   
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;
wire.cpp

class Node;

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

public:
    Wire(Node* a, Node* b);
    Node* getInput();
    Node* getOutput();

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

}

Node* Wire::getInput(){
    cout<<"\nInput: "<<input;
    return input;
}

Node* Wire::getOutput(){
    return output;
}
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);            


};
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."
}   
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;
节点.cpp

class Node;

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

public:
    Wire(Node* a, Node* b);
    Node* getInput();
    Node* getOutput();

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

}

Node* Wire::getInput(){
    cout<<"\nInput: "<<input;
    return input;
}

Node* Wire::getOutput(){
    return output;
}
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);            


};
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."
}   
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;
是标识内存映射上特定字节的整数值

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

就像这个问题:

编辑:

更新代码后,我可以看到您没有初始化
输入
输出
属性

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

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

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


cout请查看我更新的问题……问题不仅仅是打印地址。
getInput()
应该返回一个有效指针,这样我就可以调用下一个方法(即
getState()
),请查看我的更新问题……问题不仅仅是打印地址。
getInput()
应该返回一个有效指针,这样我就可以调用下一个方法(即
getState()
)@user2756695。为了帮助您,我们需要有关如何初始化
input
属性的更多信息。我已经更新了
node.cpp
的代码,其中也包含
getState()是的,这解决了问题,但您能告诉我为什么需要这样做吗?@user2756695因为否则,您不会在程序中告诉
输入
获取
a
值,也不会告诉
输出
正在获取
b
值。您必须在
Wire
对象中分配
input
一个
output
值,如何分配?通过在类构造函数中分配它们,使用哪些值?您正在作为构造函数参数传递的那些。要粘贴完整的程序吗?@tenfour:我已经发布了失败的函数
getInput()
应该返回指针,但它不是。粘贴的代码没有问题。问题出在其他地方,这就是为什么人们可能希望看到完整的程序。@user2756695我假设
input
属性是用
a
值初始化的。如果是这种情况,请检查
a
的实际参数是否为有效地址。我想您确定
输入
向量不是空的?无论如何,最好在
Node::eval()
if(!inputs.empty()){your_code}
中为它添加一个检查,告诉我们如何创建导线和节点。