Still some trouble with overloading <<

So, I'm nearing the completion of this personal project and decided to rewrite some code then review the results with an object dump to an outstream, but overloading << is still rather mysterious to me. I thought I had gotten it down, even referenced my deitel and deitel to make sure, but I'm getting accessibility issues.

geometry.h
1
2
3
4
5
6
7
8
9
10
11
12

class Geometry
{
private:
	list<Vector *> vertex_list;
	list<Triangle *> triangle_list;
	
	Vector boundingbox[2];
public:

	friend ostream& operator<<(ostream &, Geometry &);
};


geometry.cpp
1
2
3
4
5
6
ostream &operator<<(ostream &outstream, const Geometry &g )
{
	for( list<Triangle *>::iterator trit = g.triangle_list.begin(); trit != g.triangle_list.end(); trit++ )
	{
	}
}


I'm getting an accessibility issue in the for loop that steps through the triangle list. It says triangle_list is not accessible.

What am I doing wrong?
You have forgot the const keyword in the friend declaration.

You will also have to use list<Triangle*>::const_iterator instead of list<Triangle*>::iterator.
Last edited on
Oh, ok. I tend to forget little things like that and VC++ reminds me I bungled.

I made the corrections and still getting the access problem.
Show your updated code.
1
2
3
4
5
6
7
8
9
10
class Geometry
{
private:
	list<Vector *> vertex_list;
	list<Triangle *> triangle_list;
	
	Vector boundingbox[2];
public:
	friend ostream& operator<<(ostream &, const Geometry &);
};


1
2
3
4
5
6
ostream &operator<<(ostream &outstream, const Geometry &g )
{
	for( list<Triangle *>::const_iterator trit = g.triangle_list.begin(); trit != g.triangle_list.end(); trit++ )
	{
	}
}


Right now, I have the member function #if 0'd out so that I can still work on other parts.
Last edited on
Is the error a compile time or a run time error? I don't see anything wrong with that code except that operator<< doesn't return anything.

When I try this it compiles:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <list>

typedef int Vector;
typedef int Triangle;
using namespace std;

class Geometry
{
private:
	list<Vector *> vertex_list;
	list<Triangle *> triangle_list;
	Vector boundingbox[2];
public:
	friend ostream& operator<<(ostream &, const Geometry &);
};

ostream &operator<<(ostream &outstream, const Geometry &g )
{
	for( list<Triangle *>::const_iterator trit = g.triangle_list.begin(); trit != g.triangle_list.end(); trit++ )
	{
	}
	return outstream;
}


Bah. Nvm. VC++ has issues with friend functions. I use the red underlining in the IDE to tell me if my syntax or spelling is wrong and often times it flags private data in non-member friend functions.

I hit the compile button and it worked fine. The red underlining should be a red herring, like the old clip-it thing they had for MS Office.

Last edited on
Topic archived. No new replies allowed.