Connecting Tech Pros Worldwide Forums | Help | Site Map

How to Call Templatise function of class

Rajan
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi All C++ experts
My problem is regarding calling templatise class function

please see the code snip shot

#include<iostream>
using namespace std
void fun(char*);


void fun(char*)
{

}


class NULLPointer
{
public:
template<class T>
operator T*()
{
return 0;
}

}

int main()
{

NULLPointer Null;
fun(Null); // Error : not able to convert NULLPointer to char*
}

problems:
(1) how to call solve above problem
(2)If suppose there is a function like in the class

template<class T>
T* test()
{
return 0;
}

How to call this function from main function

Thanks in advance
Raj


Peter Koch Larsen
Guest
 
Posts: n/a
#2: Jul 23 '05

re: How to Call Templatise function of class



"Rajan" <Sharma.Rajendra1@gmail.com> skrev i en meddelelse
news:1117107007.006029.68070@g44g2000cwa.googlegro ups.com...[color=blue]
> Hi All C++ experts
> My problem is regarding calling templatise class function
>
> please see the code snip shot
>
> #include<iostream>
> using namespace std
> void fun(char*);
>
>
> void fun(char*)
> {
>
> }
>
>
> class NULLPointer
> {
> public:
> template<class T>
> operator T*()
> {
> return 0;
> }
>
> }
>
> int main()
> {
>
> NULLPointer Null;
> fun(Null); // Error : not able to convert NULLPointer to char*
> }
>
> problems:
> (1) how to call solve above problem[/color]

I see nothing wrong with your code. Perhaps you're using an ancient
compiler - VC++ 6.0 might well have problems here.
[color=blue]
> (2)If suppose there is a function like in the class
>
> template<class T>
> T* test()
> {
> return 0;
> }
>
> How to call this function from main function[/color]

e.g. like test<int>();[color=blue]
>
> Thanks in advance
> Raj
>[/color]

/Peter


Victor Bazarov
Guest
 
Posts: n/a
#3: Jul 23 '05

re: How to Call Templatise function of class


Peter Koch Larsen wrote:[color=blue]
> "Rajan" <Sharma.Rajendra1@gmail.com> skrev i en meddelelse
> news:1117107007.006029.68070@g44g2000cwa.googlegro ups.com...
>[color=green]
>>Hi All C++ experts
>>My problem is regarding calling templatise class function
>>
>>please see the code snip shot
>>
>>#include<iostream>
>>using namespace std
>>void fun(char*);
>>
>>
>>void fun(char*)
>>{
>>
>>}
>>
>>
>>class NULLPointer
>>{
>>public:
>> template<class T>
>> operator T*()
>> {
>> return 0;
>> }
>>
>>}
>>
>>int main()
>>{
>>
>>NULLPointer Null;
>>fun(Null); // Error : not able to convert NULLPointer to char*
>>}
>>
>>problems:
>>(1) how to call solve above problem[/color]
>
>
> I see nothing wrong with your code. Perhaps you're using an ancient
> compiler - VC++ 6.0 might well have problems here.[/color]

I can see that at least the class 'NULLPointer' definition does not end
with a semicolon. That makes the code as posted ill-formed.

To the OP: please post real code by using 'copy-and-paste'.
[color=blue]
> [..][/color]
Peter Koch Larsen
Guest
 
Posts: n/a
#4: Jul 23 '05

re: How to Call Templatise function of class



"Victor Bazarov" <v.Abazarov@comAcast.net> skrev i en meddelelse
news:%rkle.77130$NC6.7620@newsread1.mlpsca01.us.to .verio.net...[color=blue]
> Peter Koch Larsen wrote:[color=green]
>> "Rajan" <Sharma.Rajendra1@gmail.com> skrev i en meddelelse
>> news:1117107007.006029.68070@g44g2000cwa.googlegro ups.com...
>>[color=darkred]
>>>Hi All C++ experts
>>>My problem is regarding calling templatise class function
>>>
>>>please see the code snip shot
>>>
>>>#include<iostream>
>>>using namespace std
>>>void fun(char*);
>>>
>>>
>>>void fun(char*)
>>>{
>>>
>>>}
>>>
>>>
>>>class NULLPointer
>>>{
>>>public:
>>> template<class T>
>>> operator T*()
>>> {
>>> return 0;
>>> }
>>>
>>>}
>>>
>>>int main()
>>>{
>>>
>>>NULLPointer Null;
>>>fun(Null); // Error : not able to convert NULLPointer to char*
>>>}
>>>
>>>problems:
>>>(1) how to call solve above problem[/color]
>>
>>
>> I see nothing wrong with your code. Perhaps you're using an ancient
>> compiler - VC++ 6.0 might well have problems here.[/color]
>
> I can see that at least the class 'NULLPointer' definition does not end
> with a semicolon. That makes the code as posted ill-formed.[/color]

Right - didn't notice that. Apart from this the code is ok, however.

/Peter[color=blue]
>
> To the OP: please post real code by using 'copy-and-paste'.
>[color=green]
>> [..][/color][/color]


Victor Bazarov
Guest
 
Posts: n/a
#5: Jul 23 '05

re: How to Call Templatise function of class


Peter Koch Larsen wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> skrev i en meddelelse
> news:%rkle.77130$NC6.7620@newsread1.mlpsca01.us.to .verio.net...
>[color=green]
>>Peter Koch Larsen wrote:
>>[color=darkred]
>>>
>>>I see nothing wrong with your code. Perhaps you're using an ancient
>>>compiler - VC++ 6.0 might well have problems here.[/color]
>>
>>I can see that at least the class 'NULLPointer' definition does not end
>>with a semicolon. That makes the code as posted ill-formed.[/color]
>
>
> Right - didn't notice that. Apart from this the code is ok, however.[/color]

Uh... Well... The inclusion of <iostream> is not necessary and 'using
namespace std' is not terminated with a semicolon as it should be. If
I remove those, add the semicolon after the class definition, then it
should compile, yes.

VC++ v6 has lots of problems with member templates. One of them is the
actual cause of the OP's troubles IMO: VC++ v6 requires a member template
to have a typed argument to deduce a template argument from it. Since
conversion operators don't have any arguments (except 'this'), VC++ v6
fails to deduce that T should be 'char'.

It works in VC++ v7.1. To the OP: upgrade!

V
Rajan
Guest
 
Posts: n/a
#6: Jul 23 '05

re: How to Call Templatise function of class


Hi All
Here is the actual code

#include <iostream>
using namespace std;
void fun(char *x);

class NULLClass
{
public :
template<class T>
operator T*() const
{
return 0;
}

};

void fun(char *x)
{


}



int main()
{

NULLClass NULLPointer;

fun(NULLPointer);


return 0;

}

i am having vc++ 6.0 compiler

Regards

red floyd
Guest
 
Posts: n/a
#7: Jul 23 '05

re: How to Call Templatise function of class


Rajan wrote:[color=blue]
>[redacted]
> i am having vc++ 6.0 compiler
>[/color]

That's your problem. VC6 is a pre-standard compiler, it is known to
have template problems, *especially* with member function templates.

Either switch to Mingw32 or upgrade to VC7.1
dennis
Guest
 
Posts: n/a
#8: Jul 23 '05

re: How to Call Templatise function of class


I think you can "trick" MSVC++ 6 into choosing the right member
function template by doing the following:

class NULLClass
{
public:
template<class T> operator T*(T* = 0) const { return 0; }
};

Closed Thread