problem with templates

K

kalki70

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 said:
::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
 
V

Victor Bazarov

kalki70 said:
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>

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
 
K

kalki70

THANKS!!!

Victor said:
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?


Try not to post wrapped text that starts with ">" or a similar character
used in quoting.


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;
}


You need 'typename' there.


Read the FAQ.

V
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top