Thread: operator + is not doing what I expected?

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    119

    operator + is not doing what I expected?

    operator + does what I expected?


    Why? From where? How did this happen?
    Is this better?

    Code:
    #include <stdio.h>
    
    
    struct Mistructur1
    {
        int x,y,z;
        Mistructur1()
        {}
        Mistructur1(int x,int y,int z):x(x),y(y),z(z)
        {}
    
        Pr()
        {
            printf("x= %d y= %d z= %d \n", x, y, z);
        }
        /* Mistructur1 operator+ (Mistructur1 m)
        {
            return Mistructur1 (this->x+m.x, this->y+m.y, this->z+m.z);
        } */
        void operator+ (Mistructur1 &m)
        {
            return Mistructur1 (this->x = this->x+m.x, this->y = this->y+m.y, this->z = this->z+m.z);
        }
    };
    
    typedef Mistructur1 M;
    
    int main(int argc, char* argv[])
    {
        M m1(10,20,30);
        M m2(15,20,25);
        M m3;
        //m3=m1+m2;
        m1+m2;
        m1.Pr();
    
    
    return 0;
    }
    Attached Files Attached Files
    Last edited by Dmy; 07-21-2017 at 09:25 AM.

  2. #2
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Code:
    void operator+ (Mistructur1 &m)
        {
            this->x = this->x+m.x; this->y = this->y+m.y; this->z = this->z+m.z;
        }

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Dmy View Post
    operator + is not doing what I expected?

    operator + does what I expected?
    You're a nutcase!

    And your code doesn't even compile, so I have no idea what you're talking about.

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    code doesn't even compile

    Compiled.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    I actually wanted to ask forgiveness for this post, I was not paying attention, I've already fixed it myself. The operator is doing what I expected him to do.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Compiled.
    With what broken compiler?

    Here is what my compiler says:
    main.cpp|217|error: ISO C++ forbids declaration of ‘Pr’ with no type [-fpermissive]|
    main.cpp||In constructor ‘Mistructur1::Mistructur1(int, int, int)’:|
    main.cpp|214|warning: declaration of ‘z’ shadows a member of ‘Mistructur1’ [-Wshadow]|
    main.cpp|211|note: shadowed declaration is here|
    main.cpp|214|warning: declaration of ‘y’ shadows a member of ‘Mistructur1’ [-Wshadow]|
    main.cpp|211|note: shadowed declaration is here|
    main.cpp|214|warning: declaration of ‘x’ shadows a member of ‘Mistructur1’ [-Wshadow]|
    main.cpp|211|note: shadowed declaration is here|
    main.cpp||In member function ‘int Mistructur1::Pr()’:|
    main.cpp|220|warning: no return statement in function returning non-void [-Wreturn-type]|
    main.cpp||In member function ‘void Mistructur1:perator+(Mistructur1&)’:|
    main.cpp|227|error: return-statement with a value, in function returning 'void' [-fpermissive]|
    main.cpp||In function ‘int main(int, char**)’:|
    main.cpp|233|warning: unused parameter ‘argc’ [-Wunused-parameter]|
    main.cpp|233|warning: unused parameter ‘argv’ [-Wunused-parameter]|

  7. #7
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    CSOs. Thank you. I'm reading this.

  8. #8
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Yes. I beg to differ inconsistencies, don't pay attention.

    I have a good compiler, he understands me.

    I have an overloaded operator in my code. He can't.
    It does not distinguish which operator to use and reports to me.
    How's the code? How does it correct for different operator overloads?


    Code:
    #include <stdio.h>
    
    
    struct Mistructur1
    {
        int x,y,z;
        Mistructur1()
        {}
        Mistructur1(int x,int y,int z):x(x),y(y),z(z)
        {}
    
        void Pr()
        {
            printf("x= %d y= %d z= %d \n", x, y, z);
        }
        Mistructur1 operator+ (Mistructur1 m)
        {
            return Mistructur1 (this->x+m.x, this->y+m.y, this->z+m.z);
        }
        void operator+ (Mistructur1 &m)
        {
            this->x = this->x+m.x; this->y = this->y+m.y; this->z = this->z+m.z;
        }
    };
    
    typedef Mistructur1 M;
    
    int main()
    {
        M m1(10,20,30);
        M m2(15,20,25);
        M m3;
        m3=m1+m2;
        m1+m2;
        m1.Pr();
    
    
    return 0;
    }
    ||=== Build: Debug in Mistructur1 (compiler: GNU GCC Compiler) ===|
    D:\Classes\Mistructur1\Mistructur1.cpp||In function 'int main()':|
    D:\Classes\Mistructur1\Mistructur1.cpp|33|error: ambiguous overload for 'operator+' (operand types are 'M' and 'M')|
    D:\Classes\Mistructur1\Mistructur1.cpp|33|note: candidates are:|
    D:\Classes\Mistructur1\Mistructur1.cpp|16|note: Mistructur1 Mistructur1:perator+(Mistructur1)|
    D:\Classes\Mistructur1\Mistructur1.cpp|20|note: void Mistructur1:perator+(Mistructur1&)|
    D:\Classes\Mistructur1\Mistructur1.cpp|34|error: ambiguous overload for 'operator+' (operand types are 'M' and 'M')|
    D:\Classes\Mistructur1\Mistructur1.cpp|34|note: candidates are:|
    D:\Classes\Mistructur1\Mistructur1.cpp|16|note: Mistructur1 Mistructur1:perator+(Mistructur1)|
    D:\Classes\Mistructur1\Mistructur1.cpp|20|note: void Mistructur1:perator+(Mistructur1&)|
    ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Sorry but you're going about this completely the wrong way.

    The compiler is mostly complaining because There are two many overloads that match your statements in main(), but they are written in ways that can't work. For example, you wrote
    m1+m2;
    but this, if it worked, would have no effect on the rest of the program.

    I think what you might have been trying to do is write operator+ in terms of operator+=?
    Code:
    #include <stdio.h>
    
    class Mistructur {
        private:
        int x_, y_, z_;
        
        public:
        Mistructur(int x = 0, int y = 0, int z = 0): x_(x), y_(y), z_(z) {}
        void operator+= (const Mistructur &m)
        {
            x_ += m.x_;
            y_ += m.y_;
            z_ += m.z_;
        }
        void Pr() const
        {
            printf ("x = %d, y = %d, z = %d\n", x_, y_, z_);
        }
    };
    
    Mistructur operator+ (const Mistructur &m, const Mistructur &n)
    {
        Mistructur res(m); // call default copy constructor
        res += n;
        return res;
    }
    
    int main()
    {
        Mistructur m1(10, 20, 30);
        Mistructur m2(15, 20, 25);
        Mistructur m3;
        
        m3 = m1 + m2;
        m3.Pr();
    }
    
    C:\Users\jk\Desktop>g++ -Wall -o adder.exe adder.cpp
    
    C:\Users\jk\Desktop>adder
    x = 25, y = 40, z = 55
    Easy right? This list of overloaded operators might help you: Operators in C and C++ - Wikipedia

  10. #10
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Thank you!!

  11. #11
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    I did not make//m1 + m2; , it's not logical or necessary.
    I'm actually checking and reading how the operators work.

    I've read about the overloading of the operators many months ago, and I forgot, I really forgot. I'm writing and remembering.
    I'm also trying to write some strange things to figure out what to do.

    Is this better?

    I did not//m1 + m2; , it's not logical and not necessary.
    I actually check to see and read how operators operate.

    Do not think bad, I read about operator overloading, many months ago, and I forgot, strongly forgot. I write and remember.
    I also try to write strange things, to understand that would be sorted out.

    Is this better?

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Dmy
    I did not make//m1 + m2; , it's not logical or necessary.
    I'm actually checking and reading how the operators work.

    I've read about the overloading of the operators many months ago, and I forgot, I really forgot. I'm writing and remembering.
    I'm also trying to write some strange things to figure out what to do.
    Okay, just remember that what you post is what everyone has to look at - can't guess at what you might know or care about, so potentially any mistake will be discussed.
    Is this better?
    The code I wrote will work, if that is what you mean. That is typically how addition (or using plus) is implemented. You can get creative, but good practices are:

    • making sure operator+ returns an object
    • making sure that operator+ doesn't change its operands
    • making sure operator+ has the minimum amount of access necessary. In my code, everything that operator+ depends on is public (like operator+=). Sometimes that is not possible, so you make operator+ a friend function. Making plus a member should be a last resort.


    Good luck revising your studies! I know it's easy to forget things in C++ if you don't use it every day.

  13. #13
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Thank you so much! I am grateful. And you're a very smart man who understands well!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-26-2016, 07:45 AM
  2. Replies: 1
    Last Post: 02-25-2013, 08:08 AM
  3. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  4. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  5. Replies: 2
    Last Post: 07-07-2008, 03:46 AM

Tags for this Thread