class :: return value (void)

Hello!

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?

Thanks a lot!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Parameter{
 private:
  void * const value;  
  const int type;

 public:

  template<class T> T get_typecast_value(){T *_v = (T*)value; return *_v;};
 
  void get_value(){    // doesn't work
    if      (TYPE_INT    == type) {get_typecast_value<int>();}
    else if (TYPE_DOUBLE == type) {get_typecast_value<double>();}
    else if (TYPE_Float  == type) {get_typecast_value<float>();}
    else if (TYPE_String == type) {get_typecast_value<string>();}
    else error_msg();
  }
};

class TestData{
 private:
  
 public:

  list<Parameter> ParameterList; 
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.
Last edited on
Hi R0mai,

thanks and ... it's pity with the data types.
Depending on what is your final goal, we could come up with a solution. There are some boost libraries which could be useful for you.
boost::any : http://www.boost.org/doc/libs/1_43_0/doc/html/any.html
boost::variant : http://www.boost.org/doc/libs/1_43_0/doc/html/variant.html
Topic archived. No new replies allowed.