I have an assignment to learn how to use boost::variant. I’m trying to create a function that asks the user for a shape type to create. Then create the requested shape and assign it to the variant and return it. I’m using a switch to accomplish this, but I’m getting a runtime error with the default statement.
I also get a warning from the compiler: “warning C4715: ‘ShapeVariant’ : not all control paths return a value”
How do I just print a string if the user enters an invalid selection?
Thanks!
#include "boost/variant.hpp"
typedef boost::variant<Point,Line,Circle> ShapeType;
ShapeType ShapeVariant()
{
cout << "Please select a Shape Type\n1: Point\n2: Line\n3: Circle\n\nSelection: ";
int choice;
cin >> choice;
switch(choice)
{
case 1: return Point(); break;
case 2: return Line(); break;
case 3: return Circle(); break;
default: cout << "Invalid selection." << endl; break;
}
}
Instead of printing a string from the function, you could instead throw an exception that the caller catches and prints the reason why the exception was thrown.
In the caller: