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

problem with templates

P: n/a
Hi,

I'm working with templates, but I have a syntax problem I don't know
how to fix.

I built a small example, so maybe you can tell me how to write
correctly this code.

I have 2 files : template.H and template.C

Template.H :

#ifndef _TEMPLATE_H_739832983298
#define _TEMPLATE_H_739832983298

template <class T>
class MyTemplate
{
class OtherClass
{
int foo;
};
T value;

public:
OtherClass changeValue(OtherClass newValue);
};

#endif

Template.C :

#include <template.H>

template <class T>
MyTemplate< T >::OtherClass MyTemplate< T >::changeValue(MyTemplate< T
>::OtherClass newValue)
{
return newValue;
}

Here is my problem, I don't know how to make the definition of method
changeValue().
If it returns and receives a simple parameter, like int, I have no
problem, but the return type and parameter type is another class
defined inside MyTemplate.

Working in Solaris, compiling with CC I get the following warning.
"/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
(Anachronism): Type names qualified by template parameters require
"typename".
"/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
(Anachronism): Type names qualified by template parameters require
"typename".

In Linux/g++ it doesn't even compile, it generates an error instead of
a warning.

template.C:4: error: expected constructor, destructor, or type
conversion before "MyTemplate"

What is the right syntax to define methods for MyTemplate, when I need
to use a parameter type defined inside MyTemplate, like "OtherClass" ?

I've been reading some tutorials on templates in internet, but couldn't
find this situation.

Thanks for any advice!

Luis

Sep 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
kalki70 wrote:
I'm working with templates, but I have a syntax problem I don't know
how to fix.

I built a small example, so maybe you can tell me how to write
correctly this code.

I have 2 files : template.H and template.C

Template.H :

#ifndef _TEMPLATE_H_739832983298
Any names that start with an underscore and a capital letter are
reserved by the implementation. IOW, do _not_ use them, you are
not allowed to. Why do you think you need the leading underscore
here?
#define _TEMPLATE_H_739832983298

template <class T>
class MyTemplate
{
class OtherClass
{
int foo;
};
T value;

public:
OtherClass changeValue(OtherClass newValue);
};

#endif

Template.C :

#include <template.H>

template <class T>
MyTemplate< T >::OtherClass MyTemplate< T >::changeValue(MyTemplate< T
>::OtherClass newValue)
Try not to post wrapped text that starts with ">" or a similar character
used in quoting.
{
return newValue;
}

Here is my problem, I don't know how to make the definition of method
changeValue().
You almost did it right. What you need is to place 'typename' in two
places in the "head" of that function:

template<class T>
typename MyTemplate<T>::Otherclass // <- there
MyTemplate<T>::changeValue(
typename MyTemplate<T>::OtherClass newValue) // <- there
{
return newValue;
}
If it returns and receives a simple parameter, like int, I have no
problem, but the return type and parameter type is another class
defined inside MyTemplate.

Working in Solaris, compiling with CC I get the following warning.
"/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
(Anachronism): Type names qualified by template parameters require
"typename".
"/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
(Anachronism): Type names qualified by template parameters require
"typename".

In Linux/g++ it doesn't even compile, it generates an error instead of
a warning.

template.C:4: error: expected constructor, destructor, or type
conversion before "MyTemplate"

What is the right syntax to define methods for MyTemplate, when I need
to use a parameter type defined inside MyTemplate, like "OtherClass" ?
You need 'typename' there.
I've been reading some tutorials on templates in internet, but
couldn't find this situation.
Read the FAQ.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 21 '06 #2

P: n/a
THANKS!!!

Victor Bazarov wrote:
kalki70 wrote:
I'm working with templates, but I have a syntax problem I don't know
how to fix.

I built a small example, so maybe you can tell me how to write
correctly this code.

I have 2 files : template.H and template.C

Template.H :

#ifndef _TEMPLATE_H_739832983298

Any names that start with an underscore and a capital letter are
reserved by the implementation. IOW, do _not_ use them, you are
not allowed to. Why do you think you need the leading underscore
here?
#define _TEMPLATE_H_739832983298

template <class T>
class MyTemplate
{
class OtherClass
{
int foo;
};
T value;

public:
OtherClass changeValue(OtherClass newValue);
};

#endif

Template.C :

#include <template.H>

template <class T>
MyTemplate< T >::OtherClass MyTemplate< T >::changeValue(MyTemplate< T
>::OtherClass newValue)

Try not to post wrapped text that starts with ">" or a similar character
used in quoting.
{
return newValue;
}

Here is my problem, I don't know how to make the definition of method
changeValue().

You almost did it right. What you need is to place 'typename' in two
places in the "head" of that function:

template<class T>
typename MyTemplate<T>::Otherclass // <- there
MyTemplate<T>::changeValue(
typename MyTemplate<T>::OtherClass newValue) // <- there
{
return newValue;
}
If it returns and receives a simple parameter, like int, I have no
problem, but the return type and parameter type is another class
defined inside MyTemplate.

Working in Solaris, compiling with CC I get the following warning.
"/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
(Anachronism): Type names qualified by template parameters require
"typename".
"/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
(Anachronism): Type names qualified by template parameters require
"typename".

In Linux/g++ it doesn't even compile, it generates an error instead of
a warning.

template.C:4: error: expected constructor, destructor, or type
conversion before "MyTemplate"

What is the right syntax to define methods for MyTemplate, when I need
to use a parameter type defined inside MyTemplate, like "OtherClass" ?

You need 'typename' there.
I've been reading some tutorials on templates in internet, but
couldn't find this situation.

Read the FAQ.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.