473,498 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 1275
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3613
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
15
3459
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
2
3978
by: Brandon | last post by:
In a templated class I wrote, there is a public typedef for a function pointer. The class has an instance of the function pointer as a private member. That pointer is declared as static so I need...
9
2170
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
3
1883
by: Samuel Burri | last post by:
Hi there I got a simple problem. As you can see in the posted source, I have two classes, where one is derived from the other. In fact, they also contain virtual functions. In the function...
18
2840
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
7
1756
by: v4vijayakumar | last post by:
Is it possible to implement member object's virtual functions, in the containing class? If not, is it possible to simulate this behavior? ex: class test { protected: virtual void fun() = 0;...
7
1928
by: Protoman | last post by:
Is this an efficient way to integrate an explicit function of x from a to b: #include <iostream> using namespace std; class Integrate { public: Integrate(long double (*f)(long double&...
16
2651
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.