473,394 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

MI and ambiguous members

Can anyone explain me why the code below produce (with mingw-g++) the
following error message:
---------------------------------------------------------------
main10.cpp: In function `int main()':
main10.cpp:23: error: request for member `getZero' is ambiguous
main10.cpp:12: error: candidates are: virtual int B::getZero(int)
main10.cpp:6: error: virtual int A::getZero()
---------------------------------------------------------------

No! member getZero is not ambiguous at all because
int A::getZero()
is completelly different from
int B::getZero(int a)

Compiler can exactly decide what member to call when I say
c.getZero()
it is A::getZero() because B::getZero(int) has parameters.

Is this a compiler (gcc) or a language limitation?

Thanks!

-----------------------------------------------------
#include <cstdio>

class A
{
public:
virtual int getZero() { return 0; }
};

class B
{
public:
virtual int getZero(int a) { return a - a; }
};

class C : public A, public B {};
int main()
{
C c;
printf("%d", c.getZero());
return 0;
}
------------------------------------------------------
Jan 4 '07 #1
4 2228
Simpler paradigm with no Multiple Inheritance
Can anyone explain me why the code below produce (with mingw-g++) the
following error message:
---------------------------------------------------------------
main10a.cpp: In function `int main()':
main10a.cpp:19: error: no matching function for call to `C::getZero()'
main10a.cpp:12: note: candidates are: virtual int C::getZero(int)
---------------------------------------------------------------

No! member getZero is not ambiguous at all because
int A::getZero()
is completelly different from
int C::getZero(int a)

Compiler can exactly decide what member to call when I say
c.getZero()
it is A::getZero() because C::getZero(int) has parameters.

Is this a compiler (gcc) or a language limitation?

Thanks!

-----------------------------------------------------
#include <cstdio>

class A
{
public:
virtual int getZero() { return 0; }
};

class C : public A
{
public:
virtual int getZero(int a) { return a-a; }
};

int main()
{
C c;
printf("%d", c.getZero());
return 0;
}
------------------------------------------------------
Jan 4 '07 #2
Chameleon wrote:
Simpler paradigm with no Multiple Inheritance
Can anyone explain me why the code below produce (with mingw-g++) the
following error message:
---------------------------------------------------------------
main10a.cpp: In function `int main()':
main10a.cpp:19: error: no matching function for call to `C::getZero()'
main10a.cpp:12: note: candidates are: virtual int C::getZero(int)
---------------------------------------------------------------

No! member getZero is not ambiguous at all because
int A::getZero()
is completelly different from
int C::getZero(int a)

Compiler can exactly decide what member to call when I say
c.getZero()
it is A::getZero() because C::getZero(int) has parameters.

Is this a compiler (gcc) or a language limitation?

Thanks!

-----------------------------------------------------
#include <cstdio>

class A
{
public:
virtual int getZero() { return 0; }
};

class C : public A
{
public:
virtual int getZero(int a) { return a-a; }
};

int main()
{
C c;
printf("%d", c.getZero());
return 0;
}
------------------------------------------------------
You're trying to use polymorphism with with the object, not a pointer
or reference to the object. Also, A::getZero() is not overridden in C,
so simply changing your definition to new a C pointer or reference
will not help you out. Your Declaration of "C c" simply has no way of
knowing A::getZero(), so it thinks you really mean C::getZero(int).

I'm not sure of your actual semantics or what you wish to do.. but two
ways to get around your error
1. Try changing your definition "C c" to "A *c = new C;" (and use ->
instead of . or=f course)
2. Keep your current declaration, and override A::getZero() in C

also... you may want to use std::cout instead of printf... but thats
not relevent to your original question.

Jan 4 '07 #3
Yan
Chameleon wrote:
Simpler paradigm with no Multiple Inheritance
Can anyone explain me why the code below produce (with mingw-g++) the
following error message:
---------------------------------------------------------------
main10a.cpp: In function `int main()':
main10a.cpp:19: error: no matching function for call to `C::getZero()'
main10a.cpp:12: note: candidates are: virtual int C::getZero(int)
---------------------------------------------------------------

No! member getZero is not ambiguous at all because
int A::getZero()
is completelly different from
int C::getZero(int a)

Compiler can exactly decide what member to call when I say
c.getZero()
it is A::getZero() because C::getZero(int) has parameters.

Is this a compiler (gcc) or a language limitation?

Thanks!

-----------------------------------------------------
#include <cstdio>

class A
{
public:
virtual int getZero() { return 0; }
};

class C : public A
{
public:
virtual int getZero(int a) { return a-a; }
};

int main()
{
C c;
printf("%d", c.getZero());
return 0;
}
------------------------------------------------------
Name hiding rules in C++ is the reason for that. Function(s) in derived
class hide function(s) with the same name in the base class, whether
virtual or not. In your case C::getZero hides A::getZero. Doesn't
matter that they have different signatures - they don't get overloaded
as someone might expect.

Jan 4 '07 #4
"Chameleon" <ch******@hotmail.comwrote in message
news:en**********@volcano1.grnet.gr
Simpler paradigm with no Multiple Inheritance
>Can anyone explain me why the code below produce (with mingw-g++) the
following error message:
---------------------------------------------------------------
main10a.cpp: In function `int main()':
main10a.cpp:19: error: no matching function for call to `C::getZero()'
main10a.cpp:12: note: candidates are: virtual int C::getZero(int)
>---------------------------------------------------------------

No! member getZero is not ambiguous at all because
int A::getZero()
is completelly different from
int C::getZero(int a)

Compiler can exactly decide what member to call when I say
c.getZero()
it is A::getZero() because C::getZero(int) has parameters.

Is this a compiler (gcc) or a language limitation?

Thanks!

-----------------------------------------------------
#include <cstdio>

class A
{
public:
virtual int getZero() { return 0; }
};

class C : public A
{
public:
virtual int getZero(int a) { return a-a; }
};

int main()
{
C c;
printf("%d", c.getZero());
return 0;
}
>------------------------------------------------------
As Yan points out regarding the second example, getZero in C hides getZero
in A. They are not treated as overloads.

You can get them treated as overloads, however, by adding

using A::getZero;

to the declaration of C.

As for your first multiple inheritance example, once again, overloads are
not made across classes. As Stroustrup points out: "When combining
essentially unrelated classes...similarity in naming typically does not
indicate a common purpose."

Because overloading is not done across classes, C++ just looks at the names
and hence reports ambiguity.

You can resolve the problem in the same way by bringing both functions into
a common scope. Change class C to:

class C : public A, public B
{
public:
using A::getZero;
using B::getZero;
};
--
John Carson
Jan 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
Hi, I am trying to create a class heirarchy similar to the following: // base interface class ICar { public: virtual void start() = 0; }; // add members to that interface, but retain base...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
2
by: Michi Henning | last post by:
Hi, the following code produces an error on the second-last line: Interface Left Sub op() End Interface Interface Right Sub op(ByVal i As Integer)
5
by: spam.baumeister | last post by:
Hi, I am running into a problem with members which have the same name in several base classes but different calling types and the comipler complaining about amiguosity. The VC++ docu list the...
7
by: Denis Samoilov | last post by:
We have a namespace N which includes enumeration ENUM1 and a class C with property C.Enum1, e.g. namespace N { public enum ENUM1{}; public class C { public ENUM1 Enum1 { get{} set{}
2
by: Alex C. Barberi | last post by:
I'm using VB.NET 2005 and DX9 SDK (Apr 2006). I created an instance of the Direct3D.Font, and when I try to call DrawText it says: 'DrawText' is ambiguous because multiple kinds of members with...
9
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
3
by: Arpan | last post by:
The following code exists in a class file named "Users.vb": Namespace Users Public Class UserDetails Public FirstName As String Public LastName As String Public UserName As String Public...
0
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.