I'm trying to write a set of functions which converts strings to their corresponding numeric value, "int, float, double..." but My templated function is not recognized by the c++ compiler.
When I call it, I get a message stating "No matching Function for call.
I was returning a function of type T from this function, but I had this error message, I've tried modifying the function so it could change the value of a variable passed by reference, but the error persists.
I've tried modifying this function but it t doesnt still works please help.
I have several versions of this function which are overriden,
as follows:
static void convertValue(eOperandType type, const std::string &value, FloatOperandType typeDeterminer, T &container)
{
try
{
container = std::stof(value);
}
catch (std::invalid_argument e)
{
throw AVMException("The value passed as number is not valid");
}
catch (std::out_of_range e)
{
char *msg = "The value passed is out of the range of the typed passed";
throw AVMException(msg);
}
catch (exception& e)
{
throw AVMException("An unexpected error occured when converting value types");
}
}
template <typename T>
static void convertValue(eOperandType type, const std::string &value, IntOperandType typeDeterminer, T &container)
{
int val = 0;
try
{
val = std::stoi(value);
switch(type)
{
case eOperandType::Int8:
if(val < -128 || val > 127)
throw AVMWarnException("Overflow for type Int8");
case eOperandType::Int16:
if(val < -32768 || val > 32,767)
throw AVMWarnException("Overflow for type Int16");
case eOperandType::Int32:
if(val < -2147483648 || val > 2147483647)
throw AVMWarnException("Overflow for type Int32");
}
container = std::stoi(value);
}
catch (std::invalid_argument e)
{
throw AVMException("The value passed as number is not valid");
}
catch (std::out_of_range e)
{
char *msg = "The value passed is out of the range of the typed passed";
throw AVMException(msg);
}
catch (exception& e)
{
throw AVMException("An unexpected error occured when converting value types");
}
}
Here is how I call the function:
int v = 0;
Converter::convertValue<int>("20", eOperandType::Int8, IntOperandType::_Int8, &v);
If I may hazard a guess that eOperandType is an enum type, then it makes sense that you can't convert "20" to it. Additionally, you pass an int* where an int& is expected. Notice your function's signature:
template <typename T>
void convertValue(eOperandType, const std::string&, IntOperandType, T&)
and compare this to how you call the function:
convertValue<int>("20", eOperandType::Int8, IntOperandType::_Int8, &v);
Here, your parameters have the following types:
const char[3], eOperandType, IntOperandType, int*
Notice the mismatch from the signature? You probably want to call it like this:
int v = 0;
Converter::convertValue<int>(eOperandType::Int8, "20", IntOperandType::_Int8, v);
where "20" is implicitly convertible to const std::string& and v, which is an lvalue of type int, can be implicitly bound to an lvalue reference to int, int&.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With