That feeling when operator overloading is broken in mingw/g++...
That feeling when operator overloading is broken in mingw/g++...
How about sharing with us your problem? What are the errors?
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Syntax highlighting is a function of your text editor and is completely unrelated to the compiler. Is your editor in C++ mode? It seems unlikely operator overloading would be broken.
Are you using an IDE? If so which one?
Last edited by oogabooga; 09-19-2013 at 03:42 PM.
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
The problem isn't just the syntax highlighting, the compiler doesn't seem to understand operator+, it thinks of it as a normal method I think.
There are no errors, the programm just gives out the wrong result.
I'm using Eclipse CDT...
That's strange because a normal method would not be allowed to have a + in the name.
Try calling it like this:
Code:c1 = c1.operator+(c2);
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
BTW, operator+ is usually implemented as a non-member function using the operator+= member function, like this:
Code:#include <iostream> class stellarobjekt{ double laenge, breite, hoehe; public: stellarobjekt(int laenge, int breite, int hoehe){ this->laenge= laenge; this->breite =breite; this->hoehe = hoehe; } stellarobjekt& operator+=(const stellarobjekt& rhs) { laenge += rhs.laenge; breite += rhs.breite; hoehe += rhs.hoehe; return *this; } friend std::ostream& operator<<(std::ostream& os, stellarobjekt& so); }; std::ostream& operator<<(std::ostream& os, stellarobjekt& so) { return os << so.laenge << ", " << so.breite << ", " << so.hoehe; } stellarobjekt operator+(const stellarobjekt& lhs, const stellarobjekt& rhs) { return stellarobjekt(lhs) += rhs; } int main() { stellarobjekt c1(300, 400, 500); stellarobjekt c2(500, 700, 8000); std::cout << c1 << '\n' << c2 << '\n'; c1 = c1 + c2; std::cout << c1 << '\n'; return 0; }
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
So I tried it out with the Cygwin Compiler and everything works fine. In conclusion, it is an issue with MinGW.
it compiles just fine for me on MinGW with G++ 4.8.1. what version of G++ do you have in your MinGW installation?
g++ --version
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
I am using MinGW 4.7.2.
It is a "normal method", i.e., an operator overloaded as a member function is a member function like any other except that it has certain constraints on the number of parameters and can be called using syntactic sugar (which was the point of operator overloading). Indeed, this:Originally Posted by Chris L. Tailor
is equivalent to:Code:c1 = c1 + c2;
If your compiler did not understand operator+, then you should get a syntax error, not an incorrect result.Code:c1 = c1.operator+(c2);
Not likely. More likely, the issue is a user error, e.g., you forgot to compile the program again after fixing a typo. The use case that you have here is so common* that a bug like this would be very obvious and certainly a showstopper.Originally Posted by Chris L. Tailor
* That said, as oogabooga noted, the way you implemented operator+ does not follow best practice.
Aye, it compiles and runs with the expected result with my copy of the MinGW port of g++ 4.6.2.Originally Posted by Elkvis
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I have a another question, is there a difference between
andCode:stellarobjekt operator++(){ //Durch das braucht man keine Temporärvariable, sondern weist die Änderung direkt dem betreffenden Objekt zu. id++; return(*this); }
?Code:stellarobjekt& operator++(){ //Durch das braucht man keine Temporärvariable, sondern weist die Änderung direkt dem betreffenden Objekt zu. id++; return(*this); }
The author in my book uses a & behind the function, but it also works without it, why?
What does a & BEHIND the datatype of a function even do?
This is the whole program:
Code:#include <iostream> using namespace std; class stellarobjekt{ int id; double laenge, breite, hoehe; public: stellarobjekt(int laenge, int breite, int hoehe){ id = 0; this->laenge= laenge; this->breite =breite; this->hoehe = hoehe; } stellarobjekt operator- (){ stellarobjekt tmp = (*this); tmp.laenge = -tmp.laenge; tmp.breite = -tmp.breite; tmp.hoehe = -tmp.hoehe; return (tmp); } //Prefix ++i, Erhöhung und DANN Zuweisung stellarobjekt operator++(){ //Durch das braucht man keine Temporärvariable, sondern weist die Änderung direkt dem betreffenden Objekt zu. id++; return(*this); } //Postfix i++, erst Zuweisung, DANN Erhöhung stellarobjekt operator++(int){ //VEREINBARUNG: Für den Postfix gibt dummy-Parameter als Integer an. stellarobjekt tmp(*this);//erstellt eine Kopie in tmp des zu inkrementierenden Objekts id++; return(tmp); } void ausgabe(){ cout << id << " " <<laenge << " " << breite << " " << hoehe << endl; } }; int main() { //Vorzeichen einer Variable ändern signed int i = 12; i = -i; cout << i << endl; //Vorzeichen eines Objekts durch Operator Overloading ändern. stellarobjekt c1(300,400,500); c1 = -c1; c1 = ++c1; //Id=1 c1.ausgabe(); c1++; //Id=2 c1.ausgabe(); return 0; }
I think, I should precise my question:
I know what a reference is, but (I know what &stellarobject means) stellarobject& , why can you write it after the data type?
And &stellarobject and stellarobject& doesn't seem to be the same...
Last edited by Chris L. Tailor; 09-20-2013 at 01:50 PM.