"error: no matching function for call to" constructor errror
"error: no matching function for call to" constructor errror
我正在尝试构建一个电路模拟器,并且 运行 出现了一些具有继承性的错误。我想我只是很困惑,因为我已经盯着它看了太久了,所以希望得到一些帮助。
错误信息:
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 导致问题:
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:
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
};
请记住,在我为 wire 添加第二个构造函数之前,代码是有效的,它接受一个 bool 和一个 binary_gate。
我推断错误来自连线中的第二个构造函数 class。这让我感到困惑,因为它与第一个构造函数非常相似,我所做的只是传递一个布尔输入,可以说这应该更易于编码!
谢谢
你的wire
class有4个成员变量。这些中的每一个都需要在构建 wire
期间构建。
wire(bool &from, binary_gate &to) : bit_output(from), gate_input(to)
给出了如何构造 bit_output
和 gate_input
的说明,但没有给出 bit_input
和 gate_output
的说明。为此调用默认构造函数。但是您没有 gate_input
类型的默认构造函数 binary_gate
.
在 binary_gate
class
中为 binary_gate
声明一个 public 默认构造函数
binary_gate() = default;
或
binary_gate() : ...initialization list... {}
如果您希望默认 binary_gate
具有其他特定值。
问题是 wire
class:
的第二个构造函数
wire(bool &from, binary_gate &to) : bit_output(from), gate_input(to) {
to.set_next_bit(from);
}
需要为 binary_gate
调用 默认构造函数 ,为 gate_output
成员...并且您没有提供默认构造函数(即没有参数的)。这是因为,一旦您提供了具有不同签名(如 binary_gate(string s)
)的构造函数,编译器就不再提供隐式默认值。来自 cppreference(加粗我的):
If no user-declared constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare
a default constructor as an inline public member of its class.
但是,wire
class 的其他构造函数对 gate_input
和 gate_output
使用 复制构造函数 ,并且编译器确实为此提供了一个默认值。同样,来自 cppreference:
If no user-defined copy constructors are provided for a class type
(struct, class, or union), the compiler will always declare a copy
constructor as a non-explicit inline public member of its class.
要解决这个问题,您需要显式设置默认构造函数(不带参数);最简单的方法如下:
class binary_gate : public logic_element{
public :
binary_gate(string s) : logic_element(s), bitA_status(false), bitB_status(false){};
binary_gate() = default; // Provide an *explicit* default constructor.
//...
随时要求进一步澄清and/or解释。
我正在尝试构建一个电路模拟器,并且 运行 出现了一些具有继承性的错误。我想我只是很困惑,因为我已经盯着它看了太久了,所以希望得到一些帮助。
错误信息:
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 导致问题:
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:
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
};
请记住,在我为 wire 添加第二个构造函数之前,代码是有效的,它接受一个 bool 和一个 binary_gate。
我推断错误来自连线中的第二个构造函数 class。这让我感到困惑,因为它与第一个构造函数非常相似,我所做的只是传递一个布尔输入,可以说这应该更易于编码!
谢谢
你的wire
class有4个成员变量。这些中的每一个都需要在构建 wire
期间构建。
wire(bool &from, binary_gate &to) : bit_output(from), gate_input(to)
给出了如何构造 bit_output
和 gate_input
的说明,但没有给出 bit_input
和 gate_output
的说明。为此调用默认构造函数。但是您没有 gate_input
类型的默认构造函数 binary_gate
.
在 binary_gate
class
binary_gate
声明一个 public 默认构造函数
binary_gate() = default;
或
binary_gate() : ...initialization list... {}
如果您希望默认 binary_gate
具有其他特定值。
问题是 wire
class:
wire(bool &from, binary_gate &to) : bit_output(from), gate_input(to) {
to.set_next_bit(from);
}
需要为 binary_gate
调用 默认构造函数 ,为 gate_output
成员...并且您没有提供默认构造函数(即没有参数的)。这是因为,一旦您提供了具有不同签名(如 binary_gate(string s)
)的构造函数,编译器就不再提供隐式默认值。来自 cppreference(加粗我的):
If no user-declared constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class.
但是,wire
class 的其他构造函数对 gate_input
和 gate_output
使用 复制构造函数 ,并且编译器确实为此提供了一个默认值。同样,来自 cppreference:
If no user-defined copy constructors are provided for a class type (struct, class, or union), the compiler will always declare a copy constructor as a non-explicit inline public member of its class.
要解决这个问题,您需要显式设置默认构造函数(不带参数);最简单的方法如下:
class binary_gate : public logic_element{
public :
binary_gate(string s) : logic_element(s), bitA_status(false), bitB_status(false){};
binary_gate() = default; // Provide an *explicit* default constructor.
//...
随时要求进一步澄清and/or解释。