Hey daniweb, I stumbled upon an issue I can't solve myself.
Here is the code :

#include <sstream>
#include <iostream>

template <class T>
inline T htot(std::string str)
    T x;
    std::stringstream ss;

    ss << std::hex << str;
    ss >> x;

    return x;

int main()
    int x = htot("0x0F");           // error: no matching function for call to 'htot(const char [5])'|

    std::cout << "Hex(F) is Dec(" << x << ")";

    return 0;

So why can't the compiler do this job without specifying the type with the call?
For example when I do specify the type with the call it compiles just fine
Call me dumb, but I just wasted 2 hours finding solutions to this.

Any help would be appreciated !
(My setup : GCC(MinGW) 4.4.1 on Win7)

Edited by Dman01: readability

5 Years
Discussion Span
Last Post by Dman01

I think you have to specify the type T. Template functions, I believe, can only deduce what T is based on the arguments passed.

The compiler does not know if you want to return an integer or float based on having std::string as an argument, for example.

int x = htot<int>("0x0F");


Ah ok, I guess the function call is proceeded before the assignment, should've known that.
Apparently the most basic errors are the most irritating ones and I don't check them ? Thanks, you saved me!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.