Karol Szkudlarek wrote:[color=blue]
>
> 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&)
>[/color]
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
[color=blue]
> but when I comment first line (typedef...) and uncomment 13 (typedef)
> line compiles fine. I can't explain such behaviour.[/color]
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.
--
Karl Heinz Buchegger
kbuchegg@gascad.at