473,386 Members | 1,962 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,386 software developers and data experts.

ambiguous call to member

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 following example:

struct A
{
void x(int i)
{
printf("\nIn A::x");
}
};

struct B
{
void x(char c)
{
printf("\nIn B::x");

}
};

struct C : A, B
{
// uncomment the following two lines to resolve this C2385
// using B::x;
// using A::x;
};

int main()
{
C aC;
aC.x(100); // C2385
aC.x('c'); // C2385
}

This also happens with types which have no valid conversions. Why that?
The compiler knows which type I am using, so why can't it choose the
correct member as it does if the two members of the same name are in
one class?

Second: The solution with the using directive given in the help does
not work if each base has several member of the same name like:

struct A
{
void x(type1);
void x(type2)
};

struct B
{
void x(type3)
void x(type4)
};

Any solution to that except using different names?

Thanks in advance for any comments.
Timm

Mar 9 '06 #1
5 1774
sp*************@googlemail.com wrote:
struct A
{
void x(int i)
{
printf("\nIn A::x");
}
};

struct B
{
void x(char c)
{
printf("\nIn B::x");

}
};

struct C : A, B
{
// uncomment the following two lines to resolve this C2385
// using B::x;
// using A::x;
};

int main()
{
C aC;
aC.x(100); // C2385
aC.x('c'); // C2385
}
A language lawyer will jump in, but I would guess that x and x do not
strictly "overload" each other, because they are not in the same scope. If
they were, their signatures (char and int, respectively) would distinguish
them.

Because they are in different scopes, both an equal distance from C, the
compiler declines to guess which one you mean.
This also happens with types which have no valid conversions. Why that?
If you were foolish or unlucky enough to name two things the same, in
production code, and if the compiler picked one, and if you refactored the
code, you would not want the compiler to change its guess and silently pick
the other one. Forcing you to declare which one you mean makes the code
more flexible.
Second: The solution with the using directive given in the help does
not work if each base has several member of the same name like:

struct A
{
void x(type1);
void x(type2)
};

struct B
{
void x(type3)
void x(type4)
};

Any solution to that except using different names?


Now they are in the same scope, and 'using' _doesn't_ distinguish them.

Use different names! That's a good style guideline in general. (I think it's
in the /Effective C++/ books.)

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 9 '06 #2
Phlip wrote:

A language lawyer will jump in, but I would guess that x and x do not
strictly "overload" each other, because they are not in the same scope. If
they were, their signatures (char and int, respectively) would distinguish
them.
Yup.

Because they are in different scopes, both an equal distance from C, the
compiler declines to guess which one you mean.


Distance doesn't affect it. If two function names are defined in
different scopes they don't overload.

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 9 '06 #3
>
Distance doesn't affect it. If two function names are defined in
different scopes they don't overload.


I must say that I am still slightly confused because in simple
inheritance,
overloading of base class methods works. So that means that the
compiler adds scopes to a derived member if and only if it was defined
in more than one base classes?

Timm

Mar 10 '06 #4
spam.baumeister wrote:
I must say that I am still slightly confused because in simple
inheritance,
overloading of base class methods works. So that means that the
compiler adds scopes to a derived member if and only if it was defined
in more than one base classes?


"Overload" only applies to one scope. Single inheritance can "hide" a method
in a parent class - that's neither overload nor "override".

In your first situation, the compiler cannot use overload lookup or scope to
pick a method, so it gives up. Like I said before, this is much better than
potentially surprising you, or your customers.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 10 '06 #5
sp*************@googlemail.com wrote:

I must say that I am still slightly confused because in simple
inheritance,
overloading of base class methods works. So that means that the
compiler adds scopes to a derived member if and only if it was defined
in more than one base classes?


struct base
{
void f(int) {}
};

struct derived : base
{
void f() {}
};

derived d;
d.f(1); // error

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 10 '06 #6

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

Similar topics

9
by: xuatla | last post by:
compile error: test1.cpp:21: error: ISO C++ says that `T mtd::CDiffOperator::getdp(const mtd::mVector&, long int, mtd::mBCTYPE) const' and `void mtd::CDiffOperator::getdp(mtd::mVector&, const...
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...
6
by: Caleb | last post by:
I have a class that has two member functions of the same name: class MyClass { public: void someFunction(int arg); void someFunction(int arg, char blah); }; I'm trying to get a...
4
by: Chameleon | last post by:
Can anyone explain me why the code below produce (with mingw-g++) the following error message: --------------------------------------------------------------- main10.cpp: In function `int main()':...
2
by: pvl_google | last post by:
Hi, I'm trying to extend an STL class with additional iterator functionality. In the simplified example below I added an extra iterator class with a dereferencing operator. This operator...
9
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class...
4
by: Joseph Turian | last post by:
I have a templated class with the following methods: Vocab(const T& t); Vocab(unsigned uid); However, when T = unsigned, and I call Vocab(unsigned(0)) then the compiler rightly complains about...
3
by: valoh | last post by:
Hi, is this legal c++ code? template <typename BaseTstruct A { BaseT& this_() { return *static_cast<BaseT*>(this); } template <typename Tvoid Foo() { this_().Bar<T>(); } template...
7
by: Ioannis Vranos | last post by:
In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html> there are some ambiguous errata, for which I propose solutions. Any comments are welcome. Ambiguous...
32
by: Anna Smidt | last post by:
I am having an "ambiguous call to overloaded function" error again. This is the function: int nGetProfWidth (int ncols, unsigned ProfSpec) { if ((ProfSpec & PROF_2d) == 0) return ncols;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.