compiling error, tried debugging but no luck

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2005
Posts: 3
Reputation: xerxes1986 is an unknown quantity at this point 
Solved Threads: 0
xerxes1986 xerxes1986 is offline Offline
Newbie Poster

compiling error, tried debugging but no luck

 
0
  #1
Apr 13th, 2005
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

  1. ostream& operator<< (ostream& os, const AirborneLocation& plane){
  2. os << plane.name << ":" << endl;
  3. os << "\tPlane's ID: " << plane.ID << endl;
  4. os << "\tPlane's Class: ";
  5. if (plane.plane_class == 'C')
  6. os << "Civilian";
  7. else
  8. os << "Military";
  9. os << endl;
  10. os << "\tPlane's Location: ";
  11. if (plane.coord_x < 0.0)
  12. os << abs(plane.coord_x) << " km west, ";
  13. else
  14. os << plane.coord_x << " km east, ";
  15. if (plane.coord_y < 0.0)
  16. os << abs(plane.coord_y) << " km south, ";
  17. else
  18. os << plane.coord_y << " km north, ";
  19. if (plane.coord_z < 0.0)
  20. os << abs(plane.coord_z) << " km down";
  21. else
  22. os << plane.coord_z << " km up";
  23. os << endl;
  24. os << "\tDistance to plane: " << plane.distance();
  25. }

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:

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

whats goin on here??
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,388
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 244
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: compiling error, tried debugging but no luck

 
0
  #2
Apr 13th, 2005
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?
  1. os << "\tDistance to plane: " << plane;
And shouldn't operator<< return os?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 3
Reputation: xerxes1986 is an unknown quantity at this point 
Solved Threads: 0
xerxes1986 xerxes1986 is offline Offline
Newbie Poster

Re: compiling error, tried debugging but no luck

 
0
  #3
Apr 13th, 2005
Originally Posted by Dave Sinkula
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?
  1. 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

  1. double AirborneLocation :: distance(){
  2. return sqrt(coord_x*coord_x + coord_y*coord_y + coord_z*coord_z);
  3. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: compiling error, tried debugging but no luck

 
0
  #4
Apr 13th, 2005
Make this change and call it good:
double AirborneLocation :: distance() const {
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 3
Reputation: xerxes1986 is an unknown quantity at this point 
Solved Threads: 0
xerxes1986 xerxes1986 is offline Offline
Newbie Poster

Re: compiling error, tried debugging but no luck

 
0
  #5
Apr 13th, 2005
Originally Posted by Narue
Make this change and call it good:
double AirborneLocation :: distance() const {
why would i need the const there?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: compiling error, tried debugging but no luck

 
0
  #6
Apr 13th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC