Connecting Tech Pros Worldwide Forums | Help | Site Map

why does not compile

Karol Szkudlarek
Guest
 
Posts: n/a
#1: Jul 22 '05
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&)


but when I comment first line (typedef...) and uncomment 13 (typedef)
line compiles fine. I can't explain such behaviour.

Karol Szkudlarek

ps.

Compiler on my box: gcc (3.3.3)


JKop
Guest
 
Posts: n/a
#2: Jul 22 '05

re: why does not compile


> typedef int my_int;[color=blue]
>
> 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;
> }[/color]



There's no such thing as A::item1.

It is not static, it is a plain old member variable.


-JKop
Karl Heinz Buchegger
Guest
 
Posts: n/a
#3: Jul 22 '05

re: why does not compile


JKop wrote:[color=blue]
>[color=green]
> > 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;
> > }[/color]
>
> There's no such thing as A::item1.
>[/color]

???
There is!
[color=blue]
> It is not static, it is a plain old member variable.[/color]
???
item1 is in the enum A::items

You may have misread the code

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Karl Heinz Buchegger
Guest
 
Posts: n/a
#4: Jul 22 '05

re: why does not compile


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
Karol Szkudlarek
Guest
 
Posts: n/a
#5: Jul 22 '05

re: why does not compile


Karl Heinz Buchegger wrote:[color=blue]
> Karol Szkudlarek wrote:
>[color=green]
>>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=green]
>>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.
>[/color]

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.

Victor Bazarov
Guest
 
Posts: n/a
#6: Jul 22 '05

re: why does not compile


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

There is at least one reason for it: a bug in the compiler.

Victor
Victor Bazarov
Guest
 
Posts: n/a
#7: Jul 22 '05

re: why does not compile


Karol Szkudlarek wrote:[color=blue]
> Hello C++ fans!
>
> Why the following program does not compile on my box:
>[/color]

Oh, forgot to mention: please don't post attachments. Thanks.
Closed Thread