why does not compile

K

Karol Szkudlarek

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)
 
J

JKop

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;
}



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

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


-JKop
 
K

Karl Heinz Buchegger

Karol said:
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.
 
K

Karol Szkudlarek

Karl said:
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




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.
 
V

Victor Bazarov

Karol said:
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.

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

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top