473,379 Members | 1,191 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

problem with templates

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
2 3526
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only...
7
by: Andy Fish | last post by:
Hi, I'm stuck with an XSL problem - can anyone give me any hints? I have some XML with nested formatting tags like this: <text> this is plain <bold> this is bold
5
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the...
11
by: ree32 | last post by:
I have a problem with XSL sorting. The problem is that I need to create a 2 column table so I am using this. <xsl:apply-templates select="//Photo"> </xsl:apply-templates> With template...
13
by: Winbatch | last post by:
Hi, If this should be directed to another group, please let me know... I've been working with templates for a few weeks and have been able to develop some nice code on solaris using the Forte C++...
11
by: Rolf Barbakken | last post by:
I have an xml with records like this one: <a:response> <a:href>http://server/public/sol/comp/1049306.eml</a:href> <a:propstat> <a:status>HTTP/1.1 200 OK</a:status> <a:prop>...
4
by: dwergkees | last post by:
Hi, Got a litte problem here. I'm trying to create a XSLT file that will do a transformation from WordML format (MS Word XML format, see...
6
by: B. Williams | last post by:
I have a problem dealing with class template where I was to write a class and after submitting the class, this is the feedback I got back from the instructor. I don't really understand it. Can...
8
by: patrik.nyman | last post by:
Consider the following document: <?xml version="1.0"?> <!DOCTYPE test> <test> <list type="index"> <item>A</item> <item>B</item> <item>C</item> <cb/>
6
by: pauldepstein | last post by:
Let double NR( double x, double(*)(const double&) f ) be the signature of a Newton-Raphson function NR. Here, f is a function which returns a double and accepts a const double&. The aim of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.