473,480 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6604

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3637
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
8
9120
by: Giulio | last post by:
I'm doing a class who takes in constructor a vector<int> keeps and makes some statistic on the vector whitout modifiing it. I would like not to make a copy of the vector for the class. to let the...
1
1496
by: Martin Magnusson | last post by:
I'm writing a custom iterator which works on a specific class and needs access to some of this class' private data. The iterator class is a friend of the other class, but I suppose that the...
5
1954
by: Active8 | last post by:
vector<double> signal; vector<double>::iterator iter; in_file.load_vector(signal); in_file.load_vector(signal.begin()); those calls give the compiler error: "could not deduce template...
3
2359
by: sd2004 | last post by:
I am still learning, could someone show/explain to me how to fix the error. I can see it is being wrong but do not know how to fix. could you also recommend a book that I can ref. to ?...
1
10874
by: Neo | last post by:
hi, This is my first post in C++ group, so please be nice to me. Thanks. Also, I will post my first C++ program following. There is a compile error, I cannot figure it out even I can fix it. And...
4
2572
by: lutorm | last post by:
Hi all, I'm having a problem writing template functions that take vector<T>::iterator as arguments and I'm sure you guys can set me straight. Like this: #include<vector> using namespace std; ...
0
1910
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*....
15
2378
by: puzzlecracker | last post by:
I see that a lot of former in the code, and wonder if there is a technical reason for that
0
7055
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
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7103
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...
1
6758
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
7010
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
5362
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,...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
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 ...
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.