ambiguous call to overloaded

I

ishekara

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 <class T>
class SPtr
{
public:
// Default constructor
SPtr() : mSptr(NULL){}
// constructor to convert. takes a different type i.e. T2
template<class T2>
SPtr(T2& p){}
// copy constructor.
template<>
SPtr(const SPtr<T>& p) {}
private:
T* mSptr;
};

void main()
{
const SPtr<int> p1;
SPtr<int> p2(p1);
}

here the SPtr<int> p2(p1) gives me an "ambiguous call to overloaded
function" error.
but if i put a const in the template for SPtr(const T2&p), it reolves the
ambiguity,
template<class T2>
SPtr(const T2& p){} // this will correct the problem.

Could someone explain this behaviour?

TFH
shekar
 
I

ishekara

here is the revised post. is this C++??

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 <class T>
class SPtr
{
public:
// Default constructor
SPtr() : mSptr(NULL){}
// constructor to convert. takes a different type i.e. T2
template<class T2>
SPtr(T2& p){}
// copy constructor.
template<>
SPtr(const SPtr<T>& p) {}
private:
T* mSptr;
};

int main()
{
const SPtr<int> p1;
SPtr<int> p2(p1);
return 0;
}

here the SPtr<int> p2(p1) gives me an "ambiguous call to overloaded
function" error.
but if i put a const in the template for SPtr(const T2&p), it reolves the
ambiguity,
template<class T2>
SPtr(const T2& p){} // this will correct the problem.

Could someone explain this behaviour?

TFH
shekar
 
S

Shezan Baig

ishekara said:
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 <class T>
class SPtr
{
public:
// Default constructor
SPtr() : mSptr(NULL){}
// constructor to convert. takes a different type i.e. T2
template<class T2>
SPtr(T2& p){}
// copy constructor.
template<>
SPtr(const SPtr<T>& p) {}



This should just be:

// copy constructor
SPtr(const SPtr& p) {}

(i.e., without "template <>" or "<T>").

Hope this helps,
-shez-
 
M

Martijn Mulder

ishekara said:
here is the revised post. is this C++??

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 <class T>
class SPtr
{
public:
// Default constructor
SPtr() : mSptr(NULL){}
// constructor to convert. takes a different type i.e. T2
template<class T2>
SPtr(T2& p){}
// copy constructor.



Remove this line from your code:
template<>




SPtr(const SPtr<T>& p) {}
private:
T* mSptr;
};

int main()
{
const SPtr<int> p1;
SPtr<int> p2(p1);
return 0;
}

here the SPtr<int> p2(p1) gives me an "ambiguous call to
overloaded function" error.
but if i put a const in the template for SPtr(const
T2&p), it reolves the ambiguity,
template<class T2>
SPtr(const T2& p){} // this will correct the problem.

Could someone explain this behaviour?

TFH
shekar



You must remove template<> before the copy constructor, see above. Yes, this is
C++
 
A

Alf P. Steinbach

* ishekara:
[top-posting]

Do not top-post. See the FAQ. Corrected.

here is the revised post. is this C++??

With regard to the declaration of 'main', yes.

The following code compiles fine with GNU and Visual C++, spot
the significant difference:


#include <cstddef>

template <class T>
class SPtr
{
public:
// Default constructor
SPtr() : mSptr(NULL){}
// constructor to convert. takes a different type i.e. T2
template<class T2>
SPtr(T2& /*p*/){}
// copy constructor.
SPtr(const SPtr<T>& /*p*/) {}
private:
T* mSptr;
};

int main()
{
const SPtr<int> p1;
SPtr<int> p2(p1);
}
 
I

ishekara

Hi all,

thanks for your reply
I removed the template<>, still it is giving ambiguous error for me. i am
using VC++ for compiling.

The ambiguity goes away by adding const in front of T2, as in
template<class T2>
SPtr(const T2& /*p*/){}

any idea why this const is required?

TFH
shekar
 
R

red floyd

ishekara said:
Hi all,

thanks for your reply
I removed the template<>, still it is giving ambiguous error for me. i am
using VC++ for compiling.

1. VC6 and VC.NET are known to have template issues. VC.NET 2K3 is
fairly good with regard to them.

2. Copy constructors should always use const references anyways, unless
there is an EXTREMELY good reason not to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top