I need some assistance understanding why "a.vector_.begin()" is not valid in the below code. I've made the below sample program to help explain it easier, I can get around it using a temporary variable and an extra statement but I was trying to understand why it wouldn't work.
I've done some searching around and have been reading through my reference book but I'm unable to understand this at the moment.
If any more experienced coders out there could explain why this compiler error occurs it would be greatly appreciated:
Comiler error:
C:\test>gpp q.cc -o q.exe
q.cc: In function 'std::ostream& operator<<(std::ostream&, const A&)':
q.cc:22: error: conversion from '__gnu_cxx::__normal_iterator<Obj* const*, std::
vector<Obj*, std::allocator<Obj*> > >' to non-scalar type '__gnu_cxx::__normal_i
terator<Obj**, std::vector<Obj*, std::allocator<Obj*> > >' requested
Sample Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <vector>
int main(int argc, char* argv[]) {
return 0;
}
class Obj {
};
class A {
public:
friend std::ostream& operator<<(std::ostream& stream, const A& a) {
// If I get the iterator by doing it like this it works ok:
std::vector<Obj*> temp = a.vector_;
std::vector<Obj*>::iterator it = temp.begin();
// But if I replace the above 2 lines of code with this (to eliminate temp) it doesn't compile:
std::vector<Obj*>::iterator it2 = a.vector_.begin();
/* Add loop here to iterate through each element of vector and put it to stream */
return stream;
}
protected:
std::vector<Obj*> vector_;
};
|