Hello, Well... actually I have a short piece of code I would like that you guys analyze. I'm using pointers to iterators but certainly I'm doing it wrong
You're treating begin and end like they're iterators... and you're doing that properly.
However you have pointers to iterators, so it's wrong.
The easiest solution is to not use pointers:
1 2 3 4 5 6 7 8 9 10
double sum_vec(vector<double>::iterator, vector<double>::iterator); // no pointers
//...
double sum_vec(vector<double>::iterator start, vector<double>::iterator end) {
double sum = 0.0;
for( ; start != end; ++start) // this is right if you have iterators
sum += *start; // only one * to deref an iterator
return sum;
}
If you want to change it to be pointers to iterators (but I really don't advise it!) you can do it this way:
1 2 3 4 5 6
double sum_vec(vector<double>::iterator* start, vector<double>::iterator* end) {
double sum = 0.0;
for( ; *start != *end; ++(*start)) // note the changes here
sum += **start;
return sum;
}
thanks for the replies, I've fixed it now. but you're right, I'm doing it for testing only, and I really need to know how could I fix the line you (guestgulkan) have so adequatly put:
for( ; start != end; ++start) //incorrect dereferencing of your pointers - 3 times
so that I can preserve my original function declaration.. that is:
Thank you very much Disch, I've noticed you've taken the time to carefully reply many of my questions, I must express my most sincere gratitude, You're really helping me to improve my abilities in the C++ programming language.