473,785 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange thing while using templates

Hi.
I have this piece of code:

=============== =============== =======
#include <map>

template <class Tclass dict
{
private:

std::map<DWORD, Tm_d;

[ ... ]

void enum_keys_as_dw ords (DWORD *out)
{
DWORD *o=out;

for (std::map<DWORD , T>::iterator it=m_d.begin(); it!=m_d.end(); it+
+)
{
*o=it->first;
o++;
};
};
};
=============== =============== =======

While compiling this in MinGW, it says:

=============== =============== =======
dict:91: error: expected `;' before "it"
=============== =============== =======

(line 91 is the line where "for (...)" is located).

I'm really don't know what to do. The problem is probably in using
type T. Where I mistaken?

Sep 8 '07 #1
6 1206
* dr*****@gmail.c om:
Hi.
I have this piece of code:

=============== =============== =======
#include <map>

template <class Tclass dict
{
private:

std::map<DWORD, Tm_d;

[ ... ]

void enum_keys_as_dw ords (DWORD *out)
{
DWORD *o=out;

for (std::map<DWORD , T>::iterator it=m_d.begin(); it!=m_d.end(); it+
+)
{
*o=it->first;
o++;
};
};
};
=============== =============== =======

While compiling this in MinGW, it says:

=============== =============== =======
dict:91: error: expected `;' before "it"
=============== =============== =======

(line 91 is the line where "for (...)" is located).

I'm really don't know what to do. The problem is probably in using
type T. Where I mistaken?
std::map<DWORD, T>::iterator could in principle be anything, depending on
the type T. So you need to inform the compiler that it's a type. You
do that by adding the word 'typename' in front.

That said, it would be a good idea to use a std::vector instead of a raw
array, and also to make that pure accessor a 'const' member function
(probably 'DWORD' is a Windows API name, but in general, reserve all
uppercase for macros).

Then the function would look like

void get_dwords( std::vector<DWO RD>& result ) const
{
typedef typename std::map<DWORD, T>::const_itera tor Iterator;
std::vector<DWO RDwords;
for( Iterator it = m_d.begin(); it != m_d.end(); ++it )
{
words.push_back ( it->first );
}
words.swap( result );
}

Note that this way is in general more exception safe as well, not just
more safe against buffer overflows, null-pointer and other problems
associated with raw arrays and pointers.

You can also provide a convenience wrapper relying on Return Value
Optimization (RVO), which most relevant compilers provide:

std::vector<DWO RDdwords() const
{
std::vector<DWO RDresult;
get_dwords( result );
return result;
}

The naming convention employed here is that a command-like function's
name 'get_dwords' says what it does, not how (e.g. enumeration) it does
it, and that a function-like function's name 'dwords' says what result
it delivers. That makes the calling code easier to read and comprehend.

Cheers, & hth.,

- Alf
Sep 8 '07 #2
In article <11************ **********@g4g2 000hsf.googlegr oups.com>,
dr*****@gmail.c om says...
Hi.
I have this piece of code:
[ code elided ]
=============== =============== =======

While compiling this in MinGW, it says:

=============== =============== =======
dict:91: error: expected `;' before "it"
=============== =============== =======

(line 91 is the line where "for (...)" is located).
I don't see anything wrong, at least in the code you posted. It's
_possible_ there's a problem in the code you elided, and it's just not
being diagnosed until you reach this point. Quite frankly, that looks
pretty unlikely -- it takes a fairly strange error for the diagnostic to
be delayed into the next function (though mis-matched braces can lead to
it getting confused about where one function ends and the next starts).
Otherwise, it looks to me like a compiler error.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 8 '07 #3
Jerry Coffin wrote:
In article <11************ **********@g4g2 000hsf.googlegr oups.com>,
dr*****@gmail.c om says...
>Hi.
I have this piece of code:

[ code elided ]
>============== =============== ========

While compiling this in MinGW, it says:

============== =============== ========
dict:91: error: expected `;' before "it"
============== =============== ========

(line 91 is the line where "for (...)" is located).

I don't see anything wrong, at least in the code you posted. It's
_possible_ there's a problem in the code you elided, and it's just not
being diagnosed until you reach this point. Quite frankly, that looks
pretty unlikely -- it takes a fairly strange error for the diagnostic to
be delayed into the next function (though mis-matched braces can lead to
it getting confused about where one function ends and the next starts).
Otherwise, it looks to me like a compiler error.
As Alf said, OP needs a "typename" ...

for (typename std::map<...>:: iterator it = ...)

Sep 8 '07 #4
In article <X7************ *****@newssvr14 .news.prodigy.n et>,
no*****@here.du de says...

[ ... ]
for (typename std::map<...>:: iterator it = ...)
Yup -- I still miss that all too often...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 9 '07 #5
Alf P. Steinbach wrote:
>
std::map<DWORD, T>::iterator could in principle be anything, depending on
the type T. So you need to inform the compiler that it's a type. You
do that by adding the word 'typename' in front.
alf, did you really ever do that? :-)
for (typename std::map<key, value>::iterato r it; ...)

the typename issue is only with template parameter

template <class Container>
void f(Contanier const& c)
{
typename Container::iter ator it;
}


--
Thanks
Barry
Sep 9 '07 #6
Barry wrote:
Alf P. Steinbach wrote:
>>
std::map<DWORD ,T>::iterator could in principle be anything, depending
on the type T. So you need to inform the compiler that it's a type.
You do that by adding the word 'typename' in front.

alf, did you really ever do that? :-)
for (typename std::map<key, value>::iterato r it; ...)

the typename issue is only with template parameter

template <class Container>
void f(Contanier const& c)
{
typename Container::iter ator it;
}
Sorry, may bad, I did't see that in the OP
there's a template parameter T
;-)

--
Thanks
Barry
Sep 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

104
4623
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.