Problem: template function in template class 
Author Message
 Problem: template function in template class

I try to make the following code on VC++ .NET but the compilation fails with
syntax error message.

template<class T> class MyClass
{
public:
    template<class T2> MyClass(const MyClass<T2>& s) { /*...*/ };
    template<class T2> int compare(const T2&);
    template<class T2> MyClass(const MyClass<T2>& s) { /*... */ };
    // ...

Quote:
};

template<class T> template<class T2> int MyClass<T>::compare(const T2& s)
{
    // ...

Quote:
}

Should I use any compiler options or may be it's a bug?


Sat, 09 Apr 2005 22:46:39 GMT  
 Problem: template function in template class
What is the error message? May it be because you declare the exact same
constructor twice?
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> I try to make the following code on VC++ .NET but the compilation
fails with
> syntax error message.

> template<class T> class MyClass
> {
> public:
>     template<class T2> MyClass(const MyClass<T2>& s) { /*...*/ };
>     template<class T2> int compare(const T2&);
>     template<class T2> MyClass(const MyClass<T2>& s) { /*... */ };
>     // ...
> };

> template<class T> template<class T2> int MyClass<T>::compare(const T2&
s)
> {
>     // ...
> }

> Should I use any compiler options or may be it's a bug?



Sat, 09 Apr 2005 23:08:23 GMT  
 Problem: template function in template class

Quote:
> What is the error message? May it be because you declare the exact same
> constructor twice?

Oh, I wish it were so... But reality is more complicated:
Here are the errors:

error C2059: syntax error : ''template<''
error C2988: unrecognizable template declaration/definition

template<class T>
template<class T2>                                 // the error appears on
this line
int MyClass<T>::compare(const T2& s)
{

Quote:
}

I've heard that g++ compiler on UNIX doesn't recognize such constructions.
May be it's the same case?
--
_______
Gruss Ilia
Quote:
> > I try to make the following code on VC++ .NET but the compilation
> fails with
> > syntax error message.

> > template<class T> class MyClass
> > {
> > public:
> >     template<class T2> MyClass(const MyClass<T2>& s) { /*...*/ };
> >     template<class T2> int compare(const T2&);
> >     template<class T2> MyClass(const MyClass<T2>& s) { /*... */ };
> >     // ...
> > };

> > template<class T> template<class T2> int MyClass<T>::compare(const T2&
> s)
> > {
> >     // ...
> > }

> > Should I use any compiler options or may be it's a bug?



Sun, 10 Apr 2005 07:59:07 GMT  
 Problem: template function in template class
Hi Ilia,
I believe that you should put the template function compare inside the class
definition (unfortunately that will make it automatically inline):

template<class T> class MyClass
{
public:
    template<class T2> int compare(const &) { /*.....*/ }
    // ...

Quote:
};

I am not sure, but I think your compiler doesn't support external definition
of template member functions. Similar things work in g++ .


Quote:
> I try to make the following code on VC++ .NET but the compilation fails
with
> syntax error message.

> template<class T> class MyClass
> {
> public:
>     template<class T2> MyClass(const MyClass<T2>& s) { /*...*/ };
>     template<class T2> int compare(const T2&);
>     template<class T2> MyClass(const MyClass<T2>& s) { /*... */ };
>     // ...
> };

> template<class T> template<class T2> int MyClass<T>::compare(const T2& s)
> {
>     // ...
> }

> Should I use any compiler options or may be it's a bug?



Sun, 10 Apr 2005 15:39:58 GMT  
 Problem: template function in template class
| error C2059: syntax error : ''template<''
| error C2988: unrecognizable template declaration/definition
|
| template<class T>
| template<class T2>              // the error appears on this line
| int MyClass<T>::compare(const T2& s)
| {
| }
|
| I've heard that g++ compiler on UNIX doesn't recognize such constructions.
| May be it's the same case?
why don't you try below:
template<class T, class T2>
int MyClass<T>::compare(const T2& s)
{    };
i have not heard such a function with two template declarations.


Sun, 10 Apr 2005 15:43:23 GMT  
 Problem: template function in template class

Quote:
> Unfortunately, VC doesn't understand this syntax - instead, you must put
the
> body of the template member inline in the class definition.  This is fixed
> in VC7.1 - the correct syntax & out-of-line definition work correctly.

Is this version already available? I havn't found any mentioning about it on
microsoft.com....


Sun, 10 Apr 2005 22:21:58 GMT  
 Problem: template function in template class

Quote:
> | error C2059: syntax error : ''template<''
> | error C2988: unrecognizable template declaration/definition
> |
> | template<class T>
> | template<class T2>              // the error appears on this line
> | int MyClass<T>::compare(const T2& s)
> | {
> | }
> |
> | I've heard that g++ compiler on UNIX doesn't recognize such
constructions.
> | May be it's the same case?
> why don't you try below:
> template<class T, class T2>
> int MyClass<T>::compare(const T2& s)
> {    };
> i have not heard such a function with two template declarations.

Because that would be wrong.  When defining the body of a template member of
a template class outside the class body, the correct syntax involes two
template<> clauses - one for the class and one for the member.

Unfortunately, VC doesn't understand this syntax - instead, you must put the
body of the template member inline in the class definition.  This is fixed
in VC7.1 - the correct syntax & out-of-line definition work correctly.

-cd



Sun, 10 Apr 2005 21:20:27 GMT  
 Problem: template function in template class

Quote:
> > Unfortunately, VC doesn't understand this syntax - instead, you must put
> the
> > body of the template member inline in the class definition.  This is
fixed
> > in VC7.1 - the correct syntax & out-of-line definition work correctly.

> Is this version already available? I havn't found any mentioning about it
on
> microsoft.com....

No, it's in private beta testing right now.  Release "soon" (IIRC,
statements like "end of this year" and "early next year" have been made by
MSFT folks on this ng).

-cd



Sun, 10 Apr 2005 22:51:27 GMT  
 Problem: template function in template class
IIRC VC++ does not yet support out of line member template function
definition for template class.

--

Ehsan Akhgari



[WWW: http://www.beginthread.com/Ehsan ]



Sun, 10 Apr 2005 17:53:06 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Problem: template function in template class

2. Problem: template function in template class

3. template functions in template classes declared outside the class definition

4. Class Template problem: The linker can't find my class template

5. template class with a template member function

6. Template function within Template Class

7. pb with template function in non template class

8. Template member functions of template classes

9. calling template member function in template class

10. Function template as member in a class template

11. template member function - non template class - VC5 - error

12. template function in a template class

 

 
Powered by phpBB® Forum Software