alright i am trying to write a program that implements a class AirborneLocation that stores info on planes in the sky yada, yada, yada....i won't post the whole src code cuz it is hella long but i will post the section that gives me the compiling error

ostream& operator<< (ostream& os, const AirborneLocation& plane){
	os << plane.name << ":" << endl;
	os << "\tPlane's ID: " << plane.ID << endl;
	os << "\tPlane's Class: ";
	if (plane.plane_class == 'C')
		os << "Civilian";
	else
		os << "Military";
	os << endl;
	os << "\tPlane's Location: ";
	if (plane.coord_x < 0.0)
		os << abs(plane.coord_x) << " km west, ";
	else
		os << plane.coord_x << " km east, ";
	if (plane.coord_y < 0.0)
		os << abs(plane.coord_y) << " km south, ";
	else
		os << plane.coord_y << " km north, ";
	if (plane.coord_z < 0.0)
		os << abs(plane.coord_z) << " km down";
	else
		os << plane.coord_z << " km up";
	os << endl;
	os << "\tDistance to plane: " << plane.distance();
}

it gives me the error:

prog7.cpp: In function `std::ostream& operator<<(std::ostream&, const
AirborneLocation&)':
prog7.cpp:117: error: passing `const AirborneLocation' as `this' argument of `
double AirborneLocation::distance()' discards qualifiers

line 117 in the program is this line:

os << "\tDistance to plane: " << plane.distance();

whats goin on here??

Recommended Answers

All 5 Replies

ostream& operator<< (ostream& os, const AirborneLocation& plane)
os << "\tDistance to plane: " << plane.distance();

Does plane.distance() return an AirborneLocation? Or should it be called like this?

os << "\tDistance to plane: " << plane;

And shouldn't operator<< return os?

ostream& operator<< (ostream& os, const AirborneLocation& plane)
os << "\tDistance to plane: " << plane.distance();

Does plane.distance() return an AirborneLocation? Or should it be called like this?

os << "\tDistance to plane: " << plane;

And shouldn't operator<< return os?

you're right i forgot the "return os;" statement....and no...plane.distance() is defined as

double AirborneLocation :: distance(){
          return sqrt(coord_x*coord_x + coord_y*coord_y + coord_z*coord_z);
}

Make this change and call it good:

double AirborneLocation :: distance() const {

Make this change and call it good:

double AirborneLocation :: distance() const {

why would i need the const there?

>why would i need the const there?
The member function doesn't change any data members, so you can safely say that it won't do so by qualifying it as const. Now you can call the member function on behalf of both const and non-const objects. The former is not possible right now because the compiler can't guarantee that the member function will not modify data members if it isn't qualified as const.

Another option is to have your overloaded operator<< take a AirborneLocation& instead of a const AirborneLocation&. However, this opens up the potential for other issues that you really don't want to deal with. ;) Qualifying any member functions that don't modify the object's state is a good habit to get into.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.