Ambiguous in MI

Discussion in 'C++' started by tas, Oct 24, 2009.

  1. tas

    tas Guest

    class B{
    public:
    virtual void fun() = 0;
    .......
    };
    class C{
    public:
    void fun();
    };
    class A: public B, private C {

    };

    void A::fun(){
    ........
    }

    MSVC 6.0 : " 'A::fun()' is ambiquous could be the 'fun' in base B of
    class A or the 'fun' in base C of class A."

    But when i wrote "public:void fun();" in class A all was good.
    Why is this happening? And how i can redefine 'C::fun()' in class A?
    tas, Oct 24, 2009
    #1
    1. Advertising

  2. * tas:
    > class B{
    > public:
    > virtual void fun() = 0;
    > ......
    > };
    > class C{
    > public:
    > void fun();
    > };
    > class A: public B, private C {
    >
    > };
    >
    > void A::fun(){
    > .......
    > }
    >
    > MSVC 6.0 : " 'A::fun()' is ambiquous could be the 'fun' in base B of
    > class A or the 'fun' in base C of class A."
    >
    > But when i wrote "public:void fun();" in class A all was good.


    I take it this means that when you provided a declaration in class A then the
    compiler finally accepted your code.


    > Why is this happening?


    C++ requires declarations.


    > And how i can redefine 'C::fun()' in class A?


    You can redefine it and that is what you've done above, but that's surely not
    what you mean. You probably mean how you can override it. To override it it
    needs to be virtual in C.


    Cheers & hth.,

    - Alf

    PS: Who or what tricked you into using a anachronism like MSVC 6.0?
    Alf P. Steinbach, Oct 24, 2009
    #2
    1. Advertising

  3. tas

    tas Guest

    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > PS: Who or what tricked you into using a anachronism like MSVC 6.0?


    my teacher for the OOP. in our class set MSVC 6.0...
    tas, Oct 24, 2009
    #3
  4. tas

    tas Guest

    thanks for all.
    I finally realized.
    tas, Oct 24, 2009
    #4
  5. tas

    James Kanze Guest

    On Oct 24, 2:28 pm, "Alf P. Steinbach" <> wrote:
    > * tas:
    > > class B{
    > > public:
    > > virtual void fun() = 0;
    > > ......
    > > };
    > > class C{
    > > public:
    > > void fun();
    > > };
    > > class A: public B, private C {
    > > };


    > > void A::fun(){
    > > .......
    > > }


    > > MSVC 6.0 : " 'A::fun()' is ambiquous could be the 'fun' in
    > > base B of class A or the 'fun' in base C of class A."


    > > But when i wrote "public:void fun();" in class A all was good.


    > I take it this means that when you provided a declaration in
    > class A then the compiler finally accepted your code.


    > > Why is this happening?


    > C++ requires declarations.


    And definitions, if the declared thing is "potentially used"
    (and any virtual function is potentially used if the class is
    instantiated).

    > > And how i can redefine 'C::fun()' in class A?


    > You can redefine it and that is what you've done above, but
    > that's surely not what you mean. You probably mean how you can
    > override it. To override it it needs to be virtual in C.


    I suspect (but I'm really just guessing) that what he's
    expecting is the behavior of Java: that C::fun() will provide
    the implementation of B::fun(), just because the function
    happens to accidentally have the same name in two different,
    unrelated classes. C++ doesn't have this defect in the
    language; you have to explicitly tell the compiler that the
    implementation in C::fun() is the one you want here, by defining
    an A::fun() which calls C::fun().

    --
    James Kanze
    James Kanze, Oct 25, 2009
    #5
  6. tas

    Pavel Guest

    James Kanze wrote:
    > On Oct 24, 2:28 pm, "Alf P. Steinbach" <> wrote:
    >> * tas:
    >>> class B{
    >>> public:
    >>> virtual void fun() = 0;
    >>> ......
    >>> };
    >>> class C{
    >>> public:
    >>> void fun();
    >>> };
    >>> class A: public B, private C {
    >>> };

    >
    >>> void A::fun(){
    >>> .......
    >>> }

    >
    >>> MSVC 6.0 : " 'A::fun()' is ambiquous could be the 'fun' in
    >>> base B of class A or the 'fun' in base C of class A."

    >
    >>> But when i wrote "public:void fun();" in class A all was good.

    >
    >> I take it this means that when you provided a declaration in
    >> class A then the compiler finally accepted your code.

    >
    >>> Why is this happening?

    >
    >> C++ requires declarations.

    >
    > And definitions, if the declared thing is "potentially used"
    > (and any virtual function is potentially used if the class is
    > instantiated).
    >
    >>> And how i can redefine 'C::fun()' in class A?

    >
    >> You can redefine it and that is what you've done above, but
    >> that's surely not what you mean. You probably mean how you can
    >> override it. To override it it needs to be virtual in C.

    >
    > I suspect (but I'm really just guessing) that what he's
    > expecting is the behavior of Java: that C::fun() will provide
    > the implementation of B::fun(), just because the function
    > happens to accidentally have the same name in two different,
    > unrelated classes. C++ doesn't have this defect in the
    > language; you have to explicitly tell the compiler that the
    > implementation in C::fun() is the one you want here, by defining
    > an A::fun() which calls C::fun().

    You are confusing me.. Care to share an example of Java code you had in
    mind?

    -Pavel

    >
    > --
    > James Kanze
    Pavel, Oct 31, 2009
    #6
  7. tas

    James Kanze Guest

    On Oct 31, 5:36 pm, Pavel
    <> wrote:
    > James Kanze wrote:


    [...]
    > > I suspect (but I'm really just guessing) that what he's
    > > expecting is the behavior of Java: that C::fun() will
    > > provide the implementation of B::fun(), just because the
    > > function happens to accidentally have the same name in two
    > > different, unrelated classes. C++ doesn't have this defect
    > > in the language; you have to explicitly tell the compiler
    > > that the implementation in C::fun() is the one you want
    > > here, by defining an A::fun() which calls C::fun().


    > You are confusing me.. Care to share an example of Java code
    > you had in mind?


    It's been a while since I've last programmed in Java, but if I
    recall correctly, something like the following is perfectly good
    Java:

    interface I
    {
    void f();
    }

    class B
    {
    public void f() {}
    }

    class D extends B implements I
    {
    }

    The B::f() overides the I::f(), even though the author of B has
    no idea what I is or expects. Given the equivalent in C++:

    class I
    {
    public:
    virtual ~I() {}
    virtual void f() = 0;
    };

    class B
    {
    public:
    void f() {}
    };

    class D : public B, public I
    {
    };

    , class D is still abstract, since the function B::f() does not
    override I::f(). The author of D must make it explicit by
    defining D::f().

    --
    James Kanze
    James Kanze, Nov 2, 2009
    #7
    1. Advertising

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. Tuukka Toivonen

    Ambiguous type?

    Tuukka Toivonen, Apr 30, 2004, in forum: VHDL
    Replies:
    3
    Views:
    722
    Stefan Frank
    May 3, 2004
  2. Replies:
    2
    Views:
    977
  3. Denon
    Replies:
    2
    Views:
    4,374
    Yuke Liew
    Jan 2, 2004
  4. Nancy.
    Replies:
    3
    Views:
    8,107
    i. Wiin
    Sep 28, 2004
  5. Ben

    Ambiguous match found.

    Ben, Apr 27, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    7,904
    bond007
    Mar 23, 2010
Loading...

Share This Page