How to Call Templatise function of class

R

Rajan

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
 
P

Peter Koch Larsen

Rajan said:
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

I see nothing wrong with your code. Perhaps you're using an ancient
compiler - VC++ 6.0 might well have problems here.
(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

e.g. like test said:
Thanks in advance
Raj

/Peter
 
V

Victor Bazarov

Peter said:
I see nothing wrong with your code. Perhaps you're using an ancient
compiler - VC++ 6.0 might well have problems here.

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'.
 
P

Peter Koch Larsen

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

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

/Peter
To the OP: please post real code by using 'copy-and-paste'.
 
V

Victor Bazarov

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

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
 
R

Rajan

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
 
R

red floyd

Rajan said:
[redacted]
i am having vc++ 6.0 compiler

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
 
D

dennis

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; }
};
 

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

Forum statistics

Threads
474,204
Messages
2,571,064
Members
47,672
Latest member
svaraho

Latest Threads

Top