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 << ")";
    std::cin.get();

    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)

Recommended Answers

All 2 Replies

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.