3

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); 
2
  • There are already functions in the standard library that achieve this (see atoi, atof and strtod). Commented Oct 26, 2018 at 17:41
  • From prototype your call should be Converter::convertValue<int>(eOperandType::Int8,"20", IntOperandType::_Int8, &v); and you forgot template <typename T> in the first function Commented Oct 26, 2018 at 17:46

2 Answers 2

2

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&.

Sign up to request clarification or add additional context in comments.

Comments

0

It could be the string coming before the e operand type when you call the function, the param lists do not match.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.