473,396 Members | 1,756 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,396 software developers and data experts.

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_dwords (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 1182
* dr*****@gmail.com:
Hi.
I have this piece of code:

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

template <class Tclass dict
{
private:

std::map<DWORD, Tm_d;

[ ... ]

void enum_keys_as_dwords (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<DWORD>& result ) const
{
typedef typename std::map<DWORD, T>::const_iterator Iterator;
std::vector<DWORDwords;
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<DWORDdwords() const
{
std::vector<DWORDresult;
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**********************@g4g2000hsf.googlegroups. com>,
dr*****@gmail.com 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**********************@g4g2000hsf.googlegroups. com>,
dr*****@gmail.com 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.net> ,
no*****@here.dude 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>::iterator it; ...)

the typename issue is only with template parameter

template <class Container>
void f(Contanier const& c)
{
typename Container::iterator 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>::iterator it; ...)

the typename issue is only with template parameter

template <class Container>
void f(Contanier const& c)
{
typename Container::iterator 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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.