| | |
compiling error, tried debugging but no luck
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2005
Posts: 3
Reputation:
Solved Threads: 0
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
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:
whats goin on here??
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
os << "\tDistance to plane: " << plane.distance();
whats goin on here??
ostream& operator<< (ostream& os, const AirborneLocation& plane)os << "\tDistance to plane: " << plane.distance(); C Syntax (Toggle Plain Text)
os << "\tDistance to plane: " << plane;
"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
•
•
Join Date: Apr 2005
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dave Sinkula
ostream& operator<< (ostream& os, const AirborneLocation& plane)Does plane.distance() return an AirborneLocation? Or should it be called like this?os << "\tDistance to plane: " << plane.distance();And shouldn't operator<< return os?C Syntax (Toggle Plain Text)
os << "\tDistance to plane: " << plane;
you're right i forgot the "return os;" statement....and no...plane.distance() is defined as
C Syntax (Toggle Plain Text)
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 { I'm here to prove you wrong.
•
•
Join Date: Apr 2005
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
Make this change and call it good:
double AirborneLocation :: distance() const {
>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.
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.
![]() |
Similar Threads
- Another "cannot find symbol" compiling error (Java)
- compiling error - help (C++)
- compiling error wtf!?!? (C)
- Please help me out, need help (C)
Other Threads in the C Forum
- Previous Thread: Assertion Error when adding edit box
- Next Thread: my first recursion program... :/
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






