Return different data type data in a single function C++

  c++, compiler-errors, function, json, templates

Please find the below code.

template<typename T>
T GetValueFromDataType(Value &value)
{
    T jsonValue;
    if (value.IsString())
    {
        assert(value.IsString());
        jsonValue = value.GetString();      //here error is showing
        return jsonValue ;
    }
    else if (value.IsInt())
    {
        assert(value.IsInt());
        jsonValue = value.GetInt();
        return jsonValue;
    }
    else if (value.IsDouble())
    {
        assert(value.IsDouble());
        jsonValue= value.GetDouble();
        return jsonValue;
    }
    else if (value.IsBool())
    {
        assert(value.IsBool());
        jsonValue = value.GetBool();
        return jsonValue;
    }
}

The jsonValue is returned in another function. There I have declared the return variable as int.Ideally it should take int case. But its giving compilation error.

Error C2440 ‘=’: cannot convert from ‘const char *’ to ‘int’

Is there any way to use the above code or anyother construct

Source: Windows Questions C++

LEAVE A COMMENT