Consider the fallowing code:
class BaseParameter
{
protected:
int value;
public:
BaseParameter(int v) : value(v) { }
virtual void print() = 0;
};
class MyParam : public BaseParameter
{
public:
MyParam(int v): BaseParameter(v) { }
void print() override
{
std::cout << "Param value: " << value << std::endl;
}
};
int main()
{
std::vector<BaseParameter> paramsVector;
paramsVector.push_back(MyParam(1));
paramsVector.push_back(MyParam(2));
paramsVector.push_back(MyParam(3));
paramsVector.push_back(MyParam(4));
for (BaseParameter& p : paramsVector)
{
p.print();
}
}
The code above is a basic representation of a more complex code I have in a project. However, this distills the problem to the basic form.
Why do I get the "cannot instantiate abstract class" error? How to over come this issue so I can use a base class and many different parameter classes and iterate and print all according to the interface?