我有创建电路SIM的分配,当我尝试使用它时,我对Notgate类有问题。
组件是一个抽象类。
class Component
{
public:
virtual bool getOutput() = 0;
virtual void prettyPrint(string padding) = 0;
virtual void linearPrint() = 0;
};
然后我有销钉和提示,它们通过组件的依赖性继承。
class Pin {
private:
bool value;
string label;
public:
Pin::Pin(string theLabel) {
label = theLabel;
}
bool Pin::getOutput() {
return value;
}
void Pin::setValue(bool newVal) {
this->value = newVal;
}
};
class NotGate {
private:
shared_ptr<Component> input;
public:
NotGate::NotGate() {
input = make_shared<Component>();
}
bool NotGate::getOutput() {
if (input == 0) {
return true;
} else {
return false;
}
}
void NotGate::setInput(shared_ptr<Component> in) {
this->input = in;
}
};
我创建了一个销钉" C"和一个Notgate" N1",我想将" C"作为" N1"的输入。当我尝试使用命令进行操作时:
n1->setInput(c);
它告诉我:No viable conversion from 'shared_ptr<Pin>' to 'shared_ptr<Component>s'
我尝试创建一个新的shated_ptr的组件和一堆不起作用的不同事物。
编译器中的错误消息很清楚。如果您想在预期shared_ptr<Component>
时能够使用shared_ptr<Pin>
,则应使Pin
成为Component
的子类。从抽象的角度来看,对我来说,Pin
是Component
的子类。
class Pin : public Component
{
...
};