Thread: Question regarding pointers

  1. #16
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    That feeling when operator overloading is broken in mingw/g++...


  2. #17
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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

  3. #18
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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

  4. #19
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    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...

  5. #20
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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

  6. #21
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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

  7. #22
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    So I tried it out with the Cygwin Compiler and everything works fine. In conclusion, it is an issue with MinGW.

  8. #23
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    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?

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Chris L. Tailor View Post
    So I tried it out with the Cygwin Compiler and everything works fine. In conclusion, it is an issue with MinGW.
    Or it could be an issue with your code because you rely on undefined behavior or implementation-defined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    I am using MinGW 4.7.2.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Chris L. Tailor
    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.
    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:
    Code:
    c1 = c1 + c2;
    is equivalent to:
    Code:
    c1 = c1.operator+(c2);
    If your compiler did not understand operator+, then you should get a syntax error, not an incorrect result.

    Quote Originally Posted by Chris L. Tailor
    In conclusion, it is an issue with MinGW.
    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.

    * That said, as oogabooga noted, the way you implemented operator+ does not follow best practice.

    Quote Originally Posted by Elkvis
    it compiles just fine for me on MinGW with G++ 4.8.1.
    Aye, it compiles and runs with the expected result with my copy of the MinGW port of g++ 4.6.2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    I have a another question, is there a difference between
    Code:
      stellarobjekt operator++(){ //Durch das  braucht man keine  Temporärvariable, sondern weist die Änderung direkt dem betreffenden  Objekt zu.
            id++;
            return(*this);
        }
    and

    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;
    }

  13. #28
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Chris L. Tailor View Post
    I have a another question, is there a difference between
    yes. the first one returns a copy of *this, while the second returns a reference to *this itself.
    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?

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Chris L. Tailor View Post
    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?
    It's called a reference. You probably want to look what a reference does. Big difference.
    However, it should be noted that they are note the same. Run

    ++(++(obj))

    Where obj is first of type int, then of your type. What do you expect to get? What do you get?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers.
    By november1992 in forum C Programming
    Replies: 4
    Last Post: 09-01-2012, 04:05 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. A question on pointers
    By Niels_M in forum C Programming
    Replies: 20
    Last Post: 08-27-2009, 08:05 AM
  4. Pointers to pointers question
    By mikahell in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2006, 12:54 PM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 11:13 PM