I want to create a general data type "Parameter", which is used in a list "ParameterList" in class TestData. It works, and I obtain the value in Parameter for example via "get_typecast_value<int>()". I would like to simplify the get-value-call in parameter by using just "get_value()". However the compiler doesn't like my construction. What's my fault?
void output_liste(list<Parameter> Liste)
{
double value;
typedef list<Parameter>::iterator LI;
for(LI i=Liste.begin() ; i != Liste.end() ; i++){
// doesn't work
value = i->get_value();
// it's fine
value = i->get_typecast_value<double>();
}
}
Error Message of the Compiler:
In function ‘void output_liste(std::list<Parameter, std::allocator<Parameter> >)’:
src//template_field.cc:84: error: void value not ignored as it ought to be
void get_value(); -> returns void
You have to strictly decide what your function will return, it can't be decided at run time. (With a small exception to inheritance). In your current implementation get_value() should be able to return either int, double, float, string, or possibly nothing. That's not going to work.