0

So I was asked to solve this question...however when i compile it, it doesn't work...could somebody tell me what's wrong and how to solve it (im guessing it's something to do with overloading since my lecturer was talking abt it just before this was given)? thanks a lot!

#include <iostream>
#include <vector>
using namespace std;
class Integer {
    public:
        int value;
};
int main() {
    vector<Integer> v;
    for( int i=0; i<10; i++ ) {
        Integer x;
        x.value = i;
        v.push_back( x );
    }
    vector<Integer>::iterator itr;
    for( itr=v.begin(); itr!=v.end(); itr+=2 )
        cout << *itr << endl;

    return 0;
}
3
Contributors
2
Replies
12
Views
3 Years
Discussion Span
Last Post by DeanMSands3
0

Line 17: cout << *itr << endl;
*itr is of type Integer, so what you want is the value member of class Integer
cout << static_cast<Integer>(*itr).value << endl;

0

Now, I'm not a terribly smart man, but on line 17, wouldn't you be trying to print the value property of the Integer that itr points to?
EDIT: Beat me to it!

Edited by DeanMSands3

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.