By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,120 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,120 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
--
Jahagirdar .V.S
IC Design Engineer , Texas Instruments (India) Ltd.
91-80-25099129(O) 91-80-28540394(R)
Aug 5 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jahagirdar Vijayvithal S wrote:
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<int>("jvs");
}
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
Aug 5 '05 #2

P: n/a
* Victor Bazarov <v.********@comAcast.net> wrote:
<snip>
template<class U> a<U>* foo(char *name);

<snip>
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<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
--
Features should be discovered, not documented.
Jahagirdar .V.S
IC Design Engineer , Texas Instruments (India) Ltd.
91-80-25099129(O) 91-80-28540394(R)
Aug 7 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.