468,513 Members | 1,506 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

passing an iterator as an argument

Hi

I am trying to pass a iterator to a function, this is the iterator and
what it does

typedef vector<string>::const_iterator vs_itr;
for(vs_itr i=vect.begin(); i!=vect.end(); ++i){
x = fix_it(*i, 2.0);
..

// fix_it(string* 1.235, 2.0) returns double 1.24
double fix_it(vector<string>::const_iterator s, double n){
double y = strtod(s->c_str(),0);
return floor( y*pow(10,n) + 0.5 ) / pow(10,n);
}

I am getting an error relating to the type problem

************************************************** **************
no matching function for call to
'gen_data::fix_it(const std::basic_string<char, std::char_traits<char>, std::allocator<char>
>&, double&)'
gen_data.h:27: note: candidates are:
double gen_data::fix_it(__gnu_cxx::__normal_iterator<cons t
std::basic_string<char, std::char_traits<char>, std::allocator<char>
>*, std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char >, double)
************************************************** **************

I tried to fix it by redefining the first argument of the function so
that it is a const vector<string>::const_iterator but that did not fix
it.
Aug 8 '06 #1
2 6175

Gary Wessle wrote:
Hi

I am trying to pass a iterator to a function, this is the iterator and
what it does

typedef vector<string>::const_iterator vs_itr;
for(vs_itr i=vect.begin(); i!=vect.end(); ++i){
x = fix_it(*i, 2.0);
Should be fix_it(i, 2.0), no?
..

// fix_it(string* 1.235, 2.0) returns double 1.24
double fix_it(vector<string>::const_iterator s, double n){
double y = strtod(s->c_str(),0);
return floor( y*pow(10,n) + 0.5 ) / pow(10,n);
}

I am getting an error relating to the type problem

************************************************** **************
no matching function for call to
'gen_data::fix_it(const std::basic_string<char, std::char_traits<char>, std::allocator<char>
&, double&)'

gen_data.h:27: note: candidates are:
double gen_data::fix_it(__gnu_cxx::__normal_iterator<cons t
std::basic_string<char, std::char_traits<char>, std::allocator<char>
*, std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char >, double)
************************************************** **************

I tried to fix it by redefining the first argument of the function so
that it is a const vector<string>::const_iterator but that did not fix
it.
Aug 9 '06 #2
In article <87************@yahoo.com>, Gary Wessle <ph****@yahoo.cam>
wrote:
Hi

I am trying to pass a iterator to a function, this is the iterator and
what it does

typedef vector<string>::const_iterator vs_itr;
for(vs_itr i=vect.begin(); i!=vect.end(); ++i){
x = fix_it(*i, 2.0);
..

// fix_it(string* 1.235, 2.0) returns double 1.24
double fix_it(vector<string>::const_iterator s, double n){
double y = strtod(s->c_str(),0);
return floor( y*pow(10,n) + 0.5 ) / pow(10,n);
}
The best solution would be:

double fix_it( const string& s, double n ) {
double y = strtod( s.c_str(), 0 );
return floor( y * pow( 10, n ) + 0.5 ) / pow( 10, n );
}

In other words, don't pass an iterator to a function unless you need to
features that the iterator provides.

If you must do so, then:

for ( vs_itr i = vect.begin(); i != vect.end(); ++i ) {
x = vix_it( i, 2.0 );
// note the asterisk isn't in the above line.
Aug 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by Grant Edwards | last post: by
8 posts views Thread by Giulio | last post: by
1 post views Thread by Martin Magnusson | last post: by
5 posts views Thread by Active8 | last post: by
3 posts views Thread by sd2004 | last post: by
4 posts views Thread by lutorm | last post: by
reply views Thread by wellingj | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.