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

operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous:

Why is it ambiguous?

------ foo.cpp ------
struct Foo
{
Foo operator* (Foo) { return Foo(); }
Foo operator* (int) const { return Foo(); }
Foo () {}
Foo (int) {}
};

int main ()
{
Foo foo1;
Foo foo2;
foo1 = foo2 * 10;
return 0;
}
---------------------
------ Compilation ------

$ gpp foo.cpp
foo.cpp: In function `int main()':
foo.cpp:13: error: ISO C++ says that these are ambiguous, even though the worst
conversion for the first is better than the worst conversion for the second:
foo.cpp:4: note: candidate 1: Foo Foo::operator*(int) const
foo.cpp:3: note: candidate 2: Foo Foo::operator*(Foo)

-------------------------

P.S. If we are using 'operator*(int)' instead of 'operator*(int) const' there is no ambiguity.

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Jul 22 '05 #1
4 2334

"Alex Vinokur" <al****@big-foot.com> skrev i en meddelelse
news:30*************@uni-berlin.de...
Why is it ambiguous?

------ foo.cpp ------
struct Foo
{
Foo operator* (Foo) { return Foo(); }
Foo operator* (int) const { return Foo(); }
Foo () {}
Foo (int) {}
};

int main ()
{
Foo foo1;
Foo foo2;
foo1 = foo2 * 10;
return 0;
}
---------------------
------ Compilation ------

$ gpp foo.cpp
foo.cpp: In function `int main()':
foo.cpp:13: error: ISO C++ says that these are ambiguous, even though the
worst
conversion for the first is better than the worst conversion for the
second:
foo.cpp:4: note: candidate 1: Foo Foo::operator*(int) const
foo.cpp:3: note: candidate 2: Foo Foo::operator*(Foo)

-------------------------

P.S. If we are using 'operator*(int)' instead of 'operator*(int) const'
there is no ambiguity.
For the first case, you need to convert 10 to a Foo, for the second you need
to convert foo2 from Foo to Foo const. Both conversions are deemed equal.

/Peter

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #2
On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
<al****@big-foot.com> wrote:
Why is it ambiguous?

------ foo.cpp ------
struct Foo
{
Foo operator* (Foo) { return Foo(); }
Foo operator* (int) const { return Foo(); }
Foo () {}
Foo (int) {}
};

int main ()
{
Foo foo1;
Foo foo2;
foo1 = foo2 * 10;
return 0;
}
---------------------
------ Compilation ------

$ gpp foo.cpp
foo.cpp: In function `int main()':
foo.cpp:13: error: ISO C++ says that these are ambiguous, even though the worst
conversion for the first is better than the worst conversion for the second:
foo.cpp:4: note: candidate 1: Foo Foo::operator*(int) const
foo.cpp:3: note: candidate 2: Foo Foo::operator*(Foo)

-------------------------

P.S. If we are using 'operator*(int)' instead of 'operator*(int) const' there is no ambiguity.


Have you tried making Foo::Foo(int) explicit?

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #3

"Bob Hairgrove" <in*****@bigfoot.com> wrote in message news:b1********************************@4ax.com...
On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
<al****@big-foot.com> wrote:
Why is it ambiguous?

------ foo.cpp ------
struct Foo
{
Foo operator* (Foo) { return Foo(); }
Foo operator* (int) const { return Foo(); }
Foo () {}
Foo (int) {}
};

int main ()
{
Foo foo1;
Foo foo2;
foo1 = foo2 * 10;
return 0;
}
---------------------
------ Compilation ------

$ gpp foo.cpp
foo.cpp: In function `int main()':
foo.cpp:13: error: ISO C++ says that these are ambiguous, even though the worst
conversion for the first is better than the worst conversion for the second:
foo.cpp:4: note: candidate 1: Foo Foo::operator*(int) const
foo.cpp:3: note: candidate 2: Foo Foo::operator*(Foo)

-------------------------

P.S. If we are using 'operator*(int)' instead of 'operator*(int) const' there is no ambiguity.


Have you tried making Foo::Foo(int) explicit?

[snip]

Thanks.

Compiler has no problem with code below.

struct Foo
{
Foo operator* (Foo) { return Foo(); }
Foo operator* (int) const { return Foo(); }
Foo () {}
explicit Foo (int) {}
};

int main ()
{
Foo foo1;
Foo foo2;
foo1 = foo2 * 10;
return 0;
}

So, Foo::Foo (int) was implicitly used in the original program (?).
Where?

If we are using 'operator*(int)' instead of 'operator*(int) const' in the _original_ program a compiler has no problem too. Why?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



Jul 22 '05 #4

"Alex Vinokur" <al****@big-foot.com> skrev i en meddelelse
news:30*************@uni-berlin.de...

"Bob Hairgrove" <in*****@bigfoot.com> wrote in message
news:b1********************************@4ax.com...
On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
<al****@big-foot.com> wrote:
>Why is it ambiguous?
>
>------ foo.cpp ------
>struct Foo
>{
> Foo operator* (Foo) { return Foo(); }
> Foo operator* (int) const { return Foo(); }
> Foo () {}
> Foo (int) {}
>};
>
>int main ()
>{
>Foo foo1;
>Foo foo2;
> foo1 = foo2 * 10;
> return 0;
>}
>---------------------
>
>
>------ Compilation ------
>
>$ gpp foo.cpp
>foo.cpp: In function `int main()':
>foo.cpp:13: error: ISO C++ says that these are ambiguous, even though
>the worst
>conversion for the first is better than the worst conversion for the
>second:
>foo.cpp:4: note: candidate 1: Foo Foo::operator*(int) const
>foo.cpp:3: note: candidate 2: Foo Foo::operator*(Foo)
>
>-------------------------
>
>P.S. If we are using 'operator*(int)' instead of 'operator*(int) const'
>there is no ambiguity.


Have you tried making Foo::Foo(int) explicit?

[snip]

Thanks.

Compiler has no problem with code below.

struct Foo
{
Foo operator* (Foo) { return Foo(); }
Foo operator* (int) const { return Foo(); }
Foo () {}
explicit Foo (int) {}
};

int main ()
{
Foo foo1;
Foo foo2;
foo1 = foo2 * 10;
return 0;
}

So, Foo::Foo (int) was implicitly used in the original program (?).
Where?

If we are using 'operator*(int)' instead of 'operator*(int) const' in the
_original_ program a compiler has no problem too. Why?

Because there would be no conversion in the operator*(int) anymore. Read my
post if you haven't already.

/Peter
Jul 22 '05 #5

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

Similar topics

15
by: Tim Clacy | last post by:
Please illuminate; what operator of class 'Event' will get matched for these two cases : Event ev1; Event ev2; // Case 1 // if (ev1) ;
11
by: vineoff | last post by:
struct C { operator const char*() { return "...."; } }; int main() { C a; 0; }
2
by: Claudius | last post by:
Hello, I have written a class A with the access operator(int) overloaded by a A-const version which returns an int by value: ------------------------------------------------ #include...
0
by: Pedro | last post by:
Hello pythonians! ;-D , I have a little problem when I expose (assisted by boost.python) classes with virtual functions, specially with operator(). In the C++ code below I test two different...
4
by: Lycan. Mao.. | last post by:
Hello, I'm trying to write a function adapter object, but it fails with the above information. Can you help me. template <typename _Predicate> struct Unary_negate { typedef typename...
1
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
5
by: Chris Forone | last post by:
hello group, why i cant make the InputIterator of the following func-temp const? template<typename InputIterator, typename OutputIterator> OutputIterator Types::Copy(InputIterator source,...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.