首页 > 解决方案 > “错误:没有匹配的函数调用”构造函数错误

问题描述

我试图构建一个电路模拟器并遇到一些带有继承性的错误。我想我只是很困惑,因为我已经盯着它太久了,所以希望能得到一些帮助。

错误信息:

prep_3.cpp: In constructor 'wire::wire(bool&, binary_gate&)':
prep_3.cpp:147:70: error: no matching function for call to 'binary_gate::binary_gate()'
   wire(bool &from, binary_gate &to) : bit_output(from), gate_input(to) {
                                                                      ^

导致问题的类:

class wire{
    public : 
        wire(binary_gate &from, binary_gate &to) : gate_output(from), gate_input(to) {
            //want to pass output of from into to.
            //therefore must set_next_bit of to taking the input as the output of from
            to.set_next_bit(from.get_output()); 
        }

        wire(bool &from, binary_gate &to) : bit_output(from), gate_input(to) {
            to.set_next_bit(from); 
        }

    private : 
        binary_gate gate_output, gate_input; 
        bool bit_output, bit_input; 
}; 

二进制门类:

class binary_gate : public logic_element{
    public : 
    binary_gate(string s) : logic_element(s), bitA_status(false), bitB_status(false){}; 

    bool get_bitA(){
        if(!bitA_status){
            cout << "A hasn't been assigned yet! please set it : " ; 
            cin >> bitA; 
            bitA_status = true; 
        }
        return bitA; 
    }

    bool get_bitB(){
        if(!bitB_status){
            cout << "B hasn't been assigned yet! please set it : " ; 
            cin >> bitB; 
            bitB_status = true; 
        }
        return bitB; 
    }

    void set_bitA(bool val){
        bitA = val; 
        bitA_status = true; 
    }

    void set_bitB(bool val){
        bitB = val; 
        bitB_status = true; 
    }

    bool get_statusA(){
        return bitA_status; 
    }


    bool get_statusB(){
        return bitB_status; 
    }

    virtual void set_next_bit(bool from_bit){ 
        if(!bitA_status){ 
            bitA = from_bit;
            this->bitA_status = true; 
        }
        else if(!bitB_status){ 
            bitB = from_bit; 
            this->bitB_status = true;           
        }
    }

    protected: 
        bool bitA;
        bool bitB; 
        bool bitA_status; // true = taken, false = empty
        bool bitB_status; // true = taken, false = empty

};

请记住,在我添加第二个带有 bool 和 binary_gate 的 wire 构造函数之前,该代码正在运行。

我推断该错误来自线类中的第二个构造函数。这让我感到困惑,因为它与第一个构造函数非常相似,我所做的只是传递一个 bool 输入,可以说它应该更易于编码!

谢谢

标签: c++classoopconstructorcircuit

解决方案


你的wire类有 4 个成员变量。这些中的每一个都需要在构建wire.

wire(bool &from, binary_gate &to) : bit_output(from), gate_input(to)

提供有关如何构造bit_outputand的说明gate_input,但不提供bit_inputand gate_output。为这些调用默认构造函数。但是您没有gate_input类型为 的默认构造函数binary_gate

binary_gatebinary_gate类中声明一个公共默认构造函数

binary_gate() = default;

或者

binary_gate() : ...initialization list... {}

如果您希望默认binary_gate值具有其他特定值。


推荐阅读