Problem with member function of a templated class returning a pointer to an object of the same class

  • Thread starter Jahagirdar Vijayvithal S
  • Start date
J

Jahagirdar Vijayvithal S

I am trying to compile the code below but get an error of

/tmp/a.cpp: In function `int main()':
/tmp/a.cpp:15: no matching function for call to `a<int>::foo(const
char[4])'

----------------Start code-------------------------

template <class T>
class a {
T var;
public:
template<class U> a<U>* foo(char *name);
};



int main()
{

a<int> aa;
aa.foo("jvs");
}
template<class T> template<class U> a<U>* a<T>::foo(char *name) {
///Some processing on "name"
return (U*)0;
}
----------------End code-------------------------


The Intent of the code is to return an object of type a<U> after
processing the string name.
Any pointers on what I am doing wrong and the meaning of the error
message?


reposted by jvs for girish

Regards
Jahagirdar Vijayvithal S
 
V

Victor Bazarov

Jahagirdar said:
I am trying to compile the code below but get an error of

/tmp/a.cpp: In function `int main()':
/tmp/a.cpp:15: no matching function for call to `a<int>::foo(const
char[4])'

----------------Start code-------------------------

template <class T>
class a {
T var;
public:
template<class U> a<U>* foo(char *name);

If you intend to pass string literals here, the argument should be
a pointer to const char.
};



int main()
{

a<int> aa;
aa.foo("jvs");

What's 'U' for this call? The 'foo' is a member template, that means
it needs _its_own_ template argument to be instantiated. How should
the compiler figure it out? It can't. There is no information for it
to deduce the 'U' type. So, you have to tell it yourself:

aa.foo said:
}
template<class T> template<class U> a<U>* a<T>::foo(char *name) {
///Some processing on "name"
return (U*)0;

What is this bogus cast doing here? The return value type is not 'U*',
it's 'a<U>*'. And in general, there is no need to cast 0 to a pointer,
the Standard conversion exist for that, so just write

return 0;
}
----------------End code-------------------------


The Intent of the code is to return an object of type a<U> after
processing the string name.
Any pointers on what I am doing wrong and the meaning of the error
message?

See above.

V
 
J

Jahagirdar Vijayvithal S

* Victor Bazarov said:
What's 'U' for this call? The 'foo' is a member template, that means
it needs _its_own_ template argument to be instantiated. How should
the compiler figure it out? It can't. There is no information for it
to deduce the 'U' type. So, you have to tell it yourself:

aa.foo<int>("jvs");
Thanks! I was struggling with this code for quite a few days and was not
able to map the error message with the absence of <int>.

Regards
Jahagirdar Vijayvithal S
 

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,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top