tags:

views:

327

answers:

3

Let's say I have this class:

class Foo {
public:
   void member(std::string s);
   void member(int64_t &n);
};

Now I want to do some thing like

int64_t value = 5;
Foo f;
f.member(value);

The problem is that the compiler (at least GCC) gets confused & believes I'm trying to call member with a string using the char* constructor:

invalid conversion from 'int64_t' to 'const char*

How could I go about calling the actual member function I want without changing the method signature? Are templates the only solution? I have tried casting without any help (which shouldn't matter since the type is already unambiguous).


Sorry - found the mistake.

The declaration was:

class Foo {
public:
    void member(std::string s);
    void member(int64_t &n);
};

Removing the by-ref solved it.

+4  A: 

Cast the argument to exactly match the argument type of the overload you want:

f.member((int64_t) value);
Nathan Kitchen
That's redundant if you are already passing a variable with the correct type.
Vitali
That won't work because he has a non-const reference parameter, though.
Johannes Schaub - litb
@unknown: But he is not passing the correct type. The parameter should be __int64_t__ he is passing __uint64_t__
Martin York
Sorry - typo in the description. That should have been int64_t.
Vitali
A: 

There may be easier ways to solve this (like with a parameter cast) but I have seen solutions that look like the following:

void (Foo::*memberProc)(int64_t) = &Foo::member;

Foo f;
((f).*(memberProc))(5);

Convoluted, yes, but it's explicit.

fbrereto
Ugly. I would sooner slit my wrists than use such a solution.
C Johnson
A: 

Sorry - found the mistake.

The declaration was:

class Foo {
public:
   void member(std::string s);
   void member(int64_t &n);
};

Removing the by-ref solved it.

Vitali
You changed the method signature which you said you didn't want to do... ah well
fbrereto
Yeah, I guess. I was thinking more along the lines of changing the function name. It's still unclear to me why the compiler was getting confused (only in my code - my attempts to reduce the code to an example failed).
Vitali