【发布时间】:2020-06-09 12:50:04
【问题描述】:
我试图构建一个电路模拟器并遇到一些继承错误。我想我只是很困惑,因为我已经盯着它太久了,所以希望能得到一些帮助。
错误信息:
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 构造函数之前,代码正在运行。
我推断错误来自wire类中的第二个构造函数。这让我很困惑,因为它与第一个构造函数非常相似,我所做的只是传递一个 bool 输入,可以说它应该更易于编码!
谢谢
【问题讨论】:
标签: c++ class oop constructor circuit