why does not compile

Discussion in 'C++' started by Karol Szkudlarek, Jun 25, 2004.

  1. 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)
     
    Karol Szkudlarek, Jun 25, 2004
    #1
    1. Advertisements

  2. Karol Szkudlarek

    JKop Guest

    > 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
     
    JKop, Jun 25, 2004
    #2
    1. Advertisements

  3. JKop wrote:
    >
    > > 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.
    >


    ???
    There is!

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

    ???
    item1 is in the enum A::items

    You may have misread the code

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jun 25, 2004
    #3
  4. 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.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jun 25, 2004
    #4
  5. 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.
     
    Karol Szkudlarek, Jun 25, 2004
    #5
  6. Karol Szkudlarek wrote:
    > 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
     
    Victor Bazarov, Jun 25, 2004
    #6
  7. Karol Szkudlarek wrote:
    > Hello C++ fans!
    >
    > Why the following program does not compile on my box:
    >


    Oh, forgot to mention: please don't post attachments. Thanks.
     
    Victor Bazarov, Jun 25, 2004
    #7
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razvan
    Replies:
    7
    Views:
    1,409
    Babu Kalakrishnan
    Aug 24, 2004
  2. JavaMan
    Replies:
    1
    Views:
    418
    Lee Weiner
    Oct 30, 2004
  3. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    717
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,369
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,837
    Smokey Grindel
    Dec 2, 2006
  6. Nagaraj
    Replies:
    1
    Views:
    1,191
    Lionel B
    Mar 1, 2007
  7. Skybuck Flying
    Replies:
    16
    Views:
    933
    tragomaskhalos
    Aug 25, 2007
  8. Paul Melis

    Why oh why does this NOT give a compile error?

    Paul Melis, Nov 5, 2007, in forum: C Programming
    Replies:
    13
    Views:
    714
    cr88192
    Nov 7, 2007
Loading...