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

Ambiguous call to overloaded function

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 an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?

Thanks,
Joseph
Dec 20 '07 #1
4 2895
On Dec 19, 11:45 pm, Joseph Turian <tur...@gmail.comwrote:
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 an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?

Thanks,
Joseph

One way is by using a dummy variable to change the member function's
signature
ie:
template < typename T >
class Test
{
...
public:
void Vocab(const T& t);
void Vocab(unsigned u, int dummy);
};

The better way might be to reduce the confusion:

template < typename T >
class Test
{
...
public:
void setT(const T& t);
void setuid(unsigned u);
};
Dec 20 '07 #2
The better way might be to reduce the confusion:
[...]
I believe that he wants to do it in a constructor.
However, when T = unsigned, and I call Vocab(unsigned(0)) then the
compiler rightly complains about an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?
Hmm, given that one is const and the other is not, does the explicit
keyword work?
Dec 20 '07 #3
Joseph Turian wrote:
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 an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?
Call it indirectly through a function template.

The most specialised function matches first, so a regular function
matches before a function template.

--
Ian Collins.
Dec 20 '07 #4
On Dec 20, 8:07 am, Ian Collins <ian-n...@hotmail.comwrote:
Joseph Turian wrote:
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 an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?

Call it indirectly through a function template.

The most specialised function matches first, so a regular function
matches before a function template.

--
Ian Collins.
Clever, but pity the poor maintenance programmer !
Giving the functions different names is less elegant but better for
posterity ...
Dec 20 '07 #5

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

Similar topics

7
by: ishekara | last post by:
Hi, I am having a class template which is used to convert from one type another. I am having a problem when i use the copy constructor with same type. code. #include "stdio.h" template...
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...
5
by: rolandz | last post by:
Hi, Maybe somebody has been fighting with the problem that I do, currently. I have a class that has method f(). The two versions of the f() method accept different objects: Int and Short. These...
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...
6
by: c1t1z3n | last post by:
hiya, i'm having this weird error on my project. As far as I know "ambiguous call to overloaded function" should only occur when the compiler must choose from several methods, but here i don't think...
1
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; void print(char c) { cout << "from print(char c) : " << c << endl; return;
1
by: Ruki | last post by:
I want to overload function to swap two others types, for example, type<T>, T* and so on, but I can't compile the following code at VC 6.0. The compiler says : error C2667: 'swap' : none of 2...
8
by: Nikola | last post by:
Hello, I'm writing a String class for C++ and I'm getting the following error message when using operator: test.cpp: In function ‘int main()’: test.cpp:7: error: ISO C++ says that these are...
3
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.