g++ - Overloading function in C++ as unsigned char and unsigned int result in ambiguous -
i have overloaded functions:
void wypisz(unsigned int32 x, int n = 1); void wypisz(unsigned char x, int n = 1);
here code rise them:
main() { wypisz((int32)(32), 7); wypisz('a', 7); return 0; }
and when try compile using g++ error:
test.cpp: in function '
int main()
':test.cpp:10:21: error: call of overloaded '
wypisz(int, int)
' ambiguouswypisz((int)(32), 7);
test.cpp:10:21: note: candidates are:
test.cpp:5:6: note:
void wypisz(unsigned int, int) void wypisz(unsigned int x, int n = 1);
test.cpp:6:6: note:
void wypisz(unsigned char, int) void wypisz(unsigned char x, int n = 1);
when remove unsigned compile fine.
is there way call method - wha ti should change in call statement? unfortunatelly can not change in declaration = must stay are.
the problem that, in function call, casting int32
, neither unsigned char
nor unsigned int32
. in fact, implicitly castable either of them (the compiler can convert automatically). however, because can convert either automatically, doesn't know which convert to, , why call ambiguous.
to call method unambiguously, cast unsigned int32
:
wypisz((unsigned int32)32, 7);
now, compiler doesn't have implicit casting, since there exact match. instead, calls right function.
Comments
Post a Comment