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

why this code doesn't work!!

P: n/a
Hi all,
Why this code does't work!!
========================
#include <iostream>
template<typename T>
class a
{
public:
typedef T var;
};

class b
{
};

template<typename T>
void connect(typename a<T>::var& v1,typename a<T>::var& v2){}

int main()
{
a<b>::var v1,v2;
connect(v1,v2);

}
=======================

During compilation with gcc(3.2.3) I get following error:-
$ g++ problem.cpp
problem.cpp: In function `int main()':
problem.cpp:19: no matching function for call to `connect(b&, b&)'

I would expect to template function connect(b&, b&) to exist.

If this is not legal c++. Is there is way way to implement suct a
functionality?
Abhishek
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
A. Saksena wrote:
Hi all,
Why this code does't work!!
========================
#include <iostream>
template<typename T>
class a
{
public:
typedef T var;
};

class b
{
};

template<typename T>
void connect(typename a<T>::var& v1,typename a<T>::var& v2){}

int main()
{
a<b>::var v1,v2;
connect(v1,v2);

}
=======================

During compilation with gcc(3.2.3) I get following error:-
$ g++ problem.cpp
problem.cpp: In function `int main()':
problem.cpp:19: no matching function for call to `connect(b&, b&)'

I would expect to template function connect(b&, b&) to exist.

If this is not legal c++. Is there is way way to implement suct a
functionality?
Abhishek


I think the correct template parameter cannot be deduced.
If you supply it explicitly it works (g++ and Sun's CC)

i.e.:
connect<b>(v1,v2);

Tom

--
__________________________________________________ ______________________
Dipl.-Ing. Thomas Maier-Komor http://www.rcs.ei.tum.de
Institute for Real-Time Computer Systems (RCS) fon +49-89-289-23578
Technische Universitaet Muenchen, D-80290 Muenchen fax +49-89-289-23555
Jul 23 '05 #2

P: n/a

A. Saksena wrote:
Hi all,
Why this code does't work!!
========================
#include <iostream>
template<typename T>
class a
{
public:
typedef T var;
};

class b
{
};

template<typename T>
void connect(typename a<T>::var& v1,typename a<T>::var& v2){}

int main()
{
a<b>::var v1,v2;
connect(v1,v2);

}
=======================

During compilation with gcc(3.2.3) I get following error:-
$ g++ problem.cpp
problem.cpp: In function `int main()':
problem.cpp:19: no matching function for call to `connect(b&, b&)'


Replace the line 19 to read:

connect<b>(v1,v2);

and the code will compile clean. The parameterized implementation of
the function connect(...) is selected only if you pass the parameter(s)
it expects.

Regards,
Joginder.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.