Iterator and operator<<

Hi All,

Just learning c++ and trying to define an operator<< that will print my object. Here is my code

header.h:
1
2
3
4
5
6
7
8
9
class PyramidSolver{
	std::list <Point> allData;

	friend std::ostream & operator << (std::ostream &, const PyramidSolver &);	

	public:
		PyramidSolver(std::list <int>);
	
};


In side the PyramidSolver.cc code I have this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
...
std::ostream & operator << (std::ostream& os, const PyramidSolver & p){	
	os << "Jake";
	
	os<<p.allData.front();	//this line works.  
	
	
	std::list <Point>::iterator it;
	it=p.allData.begin();	//this line does not complile 
							//the compiler spews about 40 
							//lines of garbadge.  

	return os;
}


...


The thing that has really confused me is that if I copy the list I can then call the begin method on it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::ostream & operator << (std::ostream& os, const PyramidSolver & p){	
	os << "Jake";
	
	
	std::list <Point> temp = p.allData;
	
	std::list <Point>::iterator it;

	it=temp.begin(); //this works.  (Strange?)
	
	
	
	return os;
}


Note: There is no copy constructor for the point class and the only constructor takes a list as a parameter. Not sure if this will effect the result.

Thanks in advance.
JS
begin() is not a const member function, so when you try to operate on const PyramidSolver &p it fails.
ooooooo, ok that makes sense.

So if I want to print the list should I take the constant out? Or is it better practice to copy the list and then iterate through it. It seems like this must be a pretty standard problem, so there is probably some normal way of dealing with this.

I am unfamiliar with the term "const member function" but to my knowledge calling begin will not alter the list. It seems silly that you can not use begin on something that is constant.
Oh and thanks for the quick reply Zhuge!

Jake
Yeah, well the reason is because .begin() returns an iterator, and you can modify the iterator, which will directly modify the list. If you declared the iterator of type const_iter (I believe) then you can use const_begin() to get a const_iter that won't let you modify the list, so you can use it on const objects.
list<> should have two begin() methods - one const (which returns a const_iterator) and one non-const (which returns an iterator).
Thanks firedraco! That is a great description of why I need to use a const iterator, however I am uncertain how I can do this. I tryed

1
2
3
4
5
6
std::ostream & operator << (std::ostream& os, /*const*/ PyramidSolver & p){			
	
	std::list <Point>::iterator it = p.allData.const_begin();
	
	return os;
}


But the compiler does not reconize the .const_begin() method.

I just looked at the list, begin page:

http://www.cplusplus.com/reference/stl/list/begin.html

It says that there are two begin functions.

1
2
3
      
iterator begin ();
const_iterator begin () const;


It seems strange to me that you can overload functions with a different return value. I know with Java at least you can only overload functions with different parameters. Anyone know how I can invoke the const iteratory instead of the non constant?

Thanks for the help,

Jake

const_iterator begin() const is automatically called if the object you are invoking it on is const.

Thanks for the reply jsmith. I think that what you mean is that this should work:

const std::list <Point>::const_iterator it = p.allData.begin();

It does.

However this does not work:

1
2
3
	
const std::list <Point>::const_iterator it;
it = p.allData.begin();


This is the function that I am trying to write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

std::ostream & operator << (std::ostream& os, const PyramidSolver & p){			
	
	const std::list <Point>::const_iterator itBegin = p.allData.begin();
	const std::list <Point>::const_iterator itEnd = p.allData.end();
	for(; itBegin!=itEnd;/*itBegin++*/){
		os << *itBegin;
	}
	
	return os;
}




But the itBegin++ does not work... Not 100% sure what the point of an iterator that you cannot increment is....

Thanks all,
Jake
Last edited on
You have too many consts. itBegin and itEnd should be of type

 
std::list<Point>::const_iterator


not const std::list<Point>::const_iterator.
Topic archived. No new replies allowed.