Template problem

  • Thread starter Daniel =?iso-8859-1?Q?Lidstr=F6m?=
  • Start date
D

Daniel =?iso-8859-1?Q?Lidstr=F6m?=

Hi,

is there anything wrong with this template function?
template<class T>
void print_percents(std::eek:stream& stream, int n, T vals)
{
if( !vals.empty() ) {
T::iterator it = vals.begin();
stream << (*it).first << '/' << int(100*((*it).second / n)) << '%';
for( ++ it; it!=vals.end(); ++ it )
stream << ' ' << (*it).first << '/' << int(100*((*it).second / n)) <<
'%';
stream << '\n';
}
}

I can compile it with VC6 but gcc 2.95-3 gives this error:
..src/Hash.h: In function `void print_percents(ostream &, int, T)':
..src/Hash.h:68: parse error before `='
I.e. the line `T::iterator it = vals.begin();'
What's going on?
Thanks!
 
D

Daniel =?iso-8859-1?Q?Lidstr=F6m?=

Hi,

is there anything wrong with this template function?
template<class T>
void print_percents(std::eek:stream& stream, int n, T vals)
{
if( !vals.empty() ) {
T::iterator it = vals.begin();
stream << (*it).first << '/' << int(100*((*it).second / n)) << '%';
for( ++ it; it!=vals.end(); ++ it )
stream << ' ' << (*it).first << '/' << int(100*((*it).second / n)) <<
'%';
stream << '\n';
}
}

I can compile it with VC6 but gcc 2.95-3 gives this error:
.src/Hash.h: In function `void print_percents(ostream &, int, T)':
.src/Hash.h:68: parse error before `='
I.e. the line `T::iterator it = vals.begin();'
What's going on?
Thanks!

Sorry, I might've left out important information. The lines I'm trying to
compile are these:
int filled....
std::map<int, int> bound;
print_percents(std::cout, filled, exact);
 
J

Jonathan Turkanis

Daniel Lidström said:
Hi,

is there anything wrong with this template function?
template<class T>
void print_percents(std::eek:stream& stream, int n, T vals)
{
if( !vals.empty() ) {
T::iterator it = vals.begin();
stream << (*it).first << '/' << int(100*((*it).second / n)) << '%';
for( ++ it; it!=vals.end(); ++ it )
stream << ' ' << (*it).first << '/' << int(100*((*it).second / n)) <<
'%';
stream << '\n';
}
}


The template has know way of knowing that T::iterator is a type name,
during initial checking, unless you tell it. You have to say

typename T::iterator it = vals.begin();

Jonathan
 
M

Martin Eisenberg

Daniel said:
template<class T>
void print_percents(std::eek:stream& stream, int n, T vals)
{
if( !vals.empty() ) {
T::iterator it = vals.begin();
stream << (*it).first << '/' << int(100*((*it).second / n))
<< '%';
for( ++ it; it!=vals.end(); ++ it )
stream << ' ' << (*it).first << '/' <<
int(100*((*it).second / n)) << '%';
stream << '\n';
}
}

You can remove the empty check and the duplicated loop body if you
put the space character after each percentage. That is,

template<class T>
void print_percents(std::eek:stream& stream, int n, T vals)
{
typename T::iterator it = vals.begin();
for(; it != vals.end(); ++it)
stream << (*it).first << '/'
<< int(100*((*it).second / n))
<< '%' << ' ';
stream << '\n';
}


Martin
 
D

Daniel =?iso-8859-1?Q?Lidstr=F6m?=

You can remove the empty check and the duplicated loop body if you
put the space character after each percentage. That is,

template<class T>
void print_percents(std::eek:stream& stream, int n, T vals)
{
typename T::iterator it = vals.begin();
for(; it != vals.end(); ++it)
stream << (*it).first << '/'
<< int(100*((*it).second / n))
<< '%' << ' ';
stream << '\n';
}


Martin

Thanks for both replies! This one, however, puts a space character at the
end of the line, something I am allergic to :)
 
M

Martin Eisenberg

Daniel said:
Thanks for both replies! This one, however, puts a space
character at the end of the line, something I am allergic to :)

I don't share your allergy, but I can actually
understand it. Maybe you like this one better?

template<class T>
void print_percents(std::eek:stream& stream, int n, T vals)
{
static const char* SEP[] = {" ", "\n"};
typename T::iterator it = vals.begin();
while(it != vals.end()) {
stream << (*it).first << '/'
<< int(100*((*it).second / n)) << '%';
stream << SEP[++it == vals.end()];
}
}
 

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
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top