Question: I am having an issue with the following code for C + + . b#include Rectangle.h #include Circle.h #include void showShape ( Shape

I am having an issue with the following code for C++.
b#include "Rectangle.h"
#include "Circle.h"
#include
void showShape(Shape* shape)
{
shape->draw();
}
//Why can you use a pointer and not a Shape object?
//I could not get this to work with a showShape(uniquePtr shape) function.
void showShape(unique_ptr shape)
{
shape->draw();
}
int main()
{
vector shapes;
Rectangle rectangle(2.0,3.0);
shapes.push_back(&rectangle);
Circle circle(4.0);
shapes.push_back(&circle);
for (int i =0; i < shapes.size(); i++)
{
showShape(shapes.at(i));
//cout << shapes.at(i)<< endl;
}
cout << endl;
vector > uniqueShapes (4);
unique_ptr rectanglePtr;
rectanglePtr = make_unique (5.0,6.0);
//rectanglePtr->draw();
unique_ptr circlePtr;
circlePtr = make_unique (7.0);
//circlePtr->draw();
uniqueShapes.at(0)= move(rectanglePtr);
uniqueShapes.at(1)= move(circlePtr);
//uniqueShapes.push_back(move(rectanglePtr));
//uniqueShapes.push_back(move(circlePtr));
//Press control + k + c to comment out large sections of code.
//Press control + k + u to undo the commented out code.
uniqueShapes.at(2)= make_unique (8.0,9.0);
uniqueShapes.at(3)= make_unique (10.0);
//uniqueShapes.push_back(make_unique (8.0,9.0));
//uniqueShapes.push_back(make_unique (10.0));
for (int i =0; i < uniqueShapes.size(); i++)
{
//showShape(uniqueShapes.at(i));
//This line above says that is has been deleted somewhere already. I do not know why.
uniqueShapes.at(i)->draw();
}
for (auto index : shapes)
{
delete index;
}
}
The T.A. said it is supposed to work the same, but did not help me solve the issue. I only have the one instance of the delete (right above). None of my header or cpp files use delete. Why is showShape(uniqueShapes.at(i)); not working?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!