Karl Heinz Buchegger wrote:
Karol Szkudlarek wrote:
Hello C++ fans!
Why the following program does not compile on my box:
typedef int my_int;
class A
{
public:
enum Items
{
item1=1000,
item2=2000
};
//typedef int my_int;
int foo(const my_int&)
{
return 0;
}
int foo(const bool&)
{
return 0;
}
};
int main()
{
A a;
a.foo(A::item1);
return 0;
}
it returns errors:
testtypedef.cpp: In function `int main()':
testtypedef.cpp:27: error: call of overloaded `foo(A::Items)' is ambiguous
testtypedef.cpp:15: error: candidates are: int A::foo(const my_int&)
testtypedef.cpp:19: error: int A::foo(const bool&)
Obviously the compiler cannot decide which way to go
* First convert A::item1 to a my_int and call function int foo( const my_int& )
* First convert A::item1 to a bool and call function int foo( const bool& )
Both ways are equally good
but when I comment first line (typedef...) and uncomment 13 (typedef)
line compiles fine. I can't explain such behaviour.
It's simple. By commenting out the first typedef, there is no longer
a global alias for my_int.
Thus in function main, the compiler has no way of converting A::item1 to an
my_int, because there simply isn't a globally my_int available. It is as
if the compiler has never heared about something called my_int.
The second typedef (the one in line 13) has moved the alias into the
scope of the class.
I added printf to foo body and program call function int A::foo(const
my_int&) not int A::foo(const bool&). After reading your response I
expected that foo with bool will be called because my_int isn't
globally available.
In the meantime I tried to compile the program with my_int in global
scope on my another box (also gcc 3.3.3) and it compiles fine as I
previously expected. In real app my_int is globally available.