c++ - “错误:没有匹配的函数调用”构造函数错误
问题描述
我试图构建一个电路模拟器并遇到一些带有继承性的错误。我想我只是很困惑,因为我已经盯着它太久了,所以希望能得到一些帮助。
错误信息:
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 输入,可以说它应该更易于编码!
谢谢
解决方案
你的wire
类有 4 个成员变量。这些中的每一个都需要在构建wire
.
wire(bool &from, binary_gate &to) : bit_output(from), gate_input(to)
提供有关如何构造bit_output
and的说明gate_input
,但不提供bit_input
and gate_output
。为这些调用默认构造函数。但是您没有gate_input
类型为 的默认构造函数binary_gate
。
binary_gate
在binary_gate
类中声明一个公共默认构造函数
binary_gate() = default;
或者
binary_gate() : ...initialization list... {}
如果您希望默认binary_gate
值具有其他特定值。
推荐阅读
- react-select - react-select:默认自动完成选项
- python - 如何使用图形 api 在 facebook 中获取受众规模
- java - 读取文件时出错:skin\comic-ui.json(内部)即使文件存在,Eclipse 也找不到文件
- html - 转换的替代方法:在 div 上缩放?
- vb.net - 如何根据复选框状态使文本框出现/消失?
- aws-lambda - AWS Lambda 中的更改需要多长时间才能在您发布的技能中生效
- vb.net - 从数据集中过滤假日表
- python - 如何查询具有不同子级别的 MongoDB 数据库?
- node.js - Mongoose 在数组字段上使用 $push 会覆盖最后一个条目
- php - 在域中发送带有德语变音符号 (äöü) 的电子邮件不适用于 symfony 3.4