passing an iterator as an argument

G

Gary Wessle

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
&, double&)'

gen_data.h:27: note: candidates are:
double gen_data::fix_it(__gnu_cxx::__normal_iterator<const
*, 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.
 
N

Noah Roberts

Gary said:
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?
 
D

Daniel T.

Gary Wessle <[email protected]> said:
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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top