template function??? 
Author Message
 template function???

I want to use operator overloading template function to
produce automatic type conversion.

When I was compiling the following code, I got an error:
cannot convert parameter 1 from 'Four' to 'Three'. but If
I use general member function(commented code) instead of
template function, it works well.

Would you please tell me what is the problem? thanks.

//------------- code --------------
#include <string>
#include <iostream>
using namespace std;

class Three {
  int i;
public:
  Three(int ii = 0, int = 0) : i(ii) { }

Quote:
};

class Four {
  int x;
public:
  Four(int xx) : x( xx) { }

  template<class T> operator T(){
    return T(x);
  }

  //operator Three() const {
  //    return Three(x);
  //}

Quote:
};

void g(Three) { }
int main(int argc, char* argv[])
{
  Four four(1) ;
  g(four) ;

  return 0;

Quote:
}



Sat, 24 Dec 2005 01:32:15 GMT  
 template function???
What compiler are you using?  This code compiles fine with VC7.1 (as it
should).

-cd

Quote:

> I want to use operator overloading template function to
> produce automatic type conversion.

> When I was compiling the following code, I got an error:
> cannot convert parameter 1 from 'Four' to 'Three'. but If
> I use general member function(commented code) instead of
> template function, it works well.

> Would you please tell me what is the problem? thanks.

> //------------- code --------------
> #include <string>
> #include <iostream>
> using namespace std;

> class Three {
>   int i;
> public:
>   Three(int ii = 0, int = 0) : i(ii) { }
> };

> class Four {
>   int x;
> public:
>   Four(int xx) : x( xx) { }

>   template<class T> operator T(){
>     return T(x);
>   }

>   //operator Three() const {
>   // return Three(x);
>   //}
> };

> void g(Three) { }
> int main(int argc, char* argv[])
> {
>   Four four(1) ;
>   g(four) ;

>   return 0;
> }



Sat, 24 Dec 2005 01:37:40 GMT  
 template function???
First of all, thanks for your response.

I'm using VC 6.0. Is there a way to let it being compiled in VC 6.0?



Quote:
> What compiler are you using?  This code compiles fine with VC7.1 (as it
> should).

> -cd


> > I want to use operator overloading template function to
> > produce automatic type conversion.

> > When I was compiling the following code, I got an error:
> > cannot convert parameter 1 from 'Four' to 'Three'. but If
> > I use general member function(commented code) instead of
> > template function, it works well.

> > Would you please tell me what is the problem? thanks.

> > //------------- code --------------
> > #include <string>
> > #include <iostream>
> > using namespace std;

> > class Three {
> >   int i;
> > public:
> >   Three(int ii = 0, int = 0) : i(ii) { }
> > };

> > class Four {
> >   int x;
> > public:
> >   Four(int xx) : x( xx) { }

> >   template<class T> operator T(){
> >     return T(x);
> >   }

> >   //operator Three() const {
> >   // return Three(x);
> >   //}
> > };

> > void g(Three) { }
> > int main(int argc, char* argv[])
> > {
> >   Four four(1) ;
> >   g(four) ;

> >   return 0;
> > }



Sat, 24 Dec 2005 01:47:36 GMT  
 template function???
Not that I know of.  VC6 had weak support for member template functions -
this is probably an area where it simply doesn't work.

-cd

Quote:

> First of all, thanks for your response.

> I'm using VC 6.0. Is there a way to let it being compiled in VC 6.0?



>> What compiler are you using?  This code compiles fine with VC7.1 (as
>> it should).

>> -cd


>>> I want to use operator overloading template function to
>>> produce automatic type conversion.

>>> When I was compiling the following code, I got an error:
>>> cannot convert parameter 1 from 'Four' to 'Three'. but If
>>> I use general member function(commented code) instead of
>>> template function, it works well.

>>> Would you please tell me what is the problem? thanks.

>>> //------------- code --------------
>>> #include <string>
>>> #include <iostream>
>>> using namespace std;

>>> class Three {
>>>   int i;
>>> public:
>>>   Three(int ii = 0, int = 0) : i(ii) { }
>>> };

>>> class Four {
>>>   int x;
>>> public:
>>>   Four(int xx) : x( xx) { }

>>>   template<class T> operator T(){
>>>     return T(x);
>>>   }

>>>   //operator Three() const {
>>>   // return Three(x);
>>>   //}
>>> };

>>> void g(Three) { }
>>> int main(int argc, char* argv[])
>>> {
>>>   Four four(1) ;
>>>   g(four) ;

>>>   return 0;
>>> }



Sat, 24 Dec 2005 07:50:30 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. C++ compiler bug: template and non template function overload resolution

2. Template function within Template Class

3. template functions in template classes declared outside the class definition

4. pb with template function in non template class

5. template function in a template class

6. Template function in non-template class

7. template arguments used within a template function - how?

8. Problem: template function in template class

9. Template function in a non-template class

10. Template class with template function

11. template function inside template class bug

12. Problem: template function in template class

 

 
Powered by phpBB® Forum Software