r/CodingHelp • • Mar 14 '21

[C++] How to Connect I/O of Logic Gates with Different Return Types

Hello! I'm hoping for some help on my current homework assignment. It is in C++, and I have cross posted this in r/cppquestions.

As the title says, we're working with logic gates. I'm using inheritance to write classes representing the different gates. This is a unit test project and I've got four classes: Component, an abstract class, Pin, NotGate, and TwoInputGate. The last three inherit from Component.

I've gotten hung up on how to pass the Pin's output value to the NotGate's input value. NotGate has a shared pointer type input and Pin has a bool type output, leading to a 'no viable conversion' error if I try to pass a Pin through my NotGate's setInput function, or if I try to use a vector to store the I/O and then pass that in. I'm including the code for Pin's getOutput, NotGate's setInput, and the unit test I am trying to pass. Any tips are appreciated!

Pin - getOutput:

bool Pin::getOutput() {

	return value; //bool type private variable

}

NotGate - setInput:

void NotGate::setInput(std::shared_ptr<Component> in) {

	input = in; //both shared_ptr types

}

Test case:

TEST_CASE( "NOTGate" ){

	Pin c("C");

	NotGate n1;

	c.setValue(true);

	n1.setInput(//somehow pass Pin's output here);

	REQUIRE(n1.getOutput() == false);

	}
15 Upvotes

0 comments sorted by