why this code doesn't work!!

A

A. Saksena

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
 
T

Thomas Maier-Komor

A. Saksena said:
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
 
J

joginder.singh

A. Saksena said:
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.
 

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
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top