Thread: Understand the overload of operators in detail

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

    Understand the overload of operators in detail

    Kind time twenty-four hours! I ask for help from the society. As we say, I stepped on the same rake again. As well, "call of overloaded 'Rozklady_na_tsili_kvadraty(const Rozklady_na_tsili_kvadraty&)' is ambiguous |
    , here all clear, the second variant has more parameters, than it is necessary. Something I do not understand really. Excuse for dullness.



    Code:
    Rozklady_na_tsili_kvadraty &operator= ( const Rozklady_na_tsili_kvadraty &ob )
        {
           Rozklady_na_tsili_kvadraty tymtsasovyj_ob(ob);
           return tymtsasovyj_ob;
        }

    Code:
    ||=== Build: Debug in mn (compiler: GNU GCC Compiler) ===|
    G:\HeloWorld\mn\mn.cpp||In member function 'Rozklady_na_tsili_kvadraty& Rozklady_na_tsili_kvadraty::operator=(const Rozklady_na_tsili_kvadraty&)':|
    G:\HeloWorld\mn\mn.cpp|49|error: call of overloaded 'Rozklady_na_tsili_kvadraty(const Rozklady_na_tsili_kvadraty&)' is ambiguous|
    G:\HeloWorld\mn\mn.cpp|41|note: candidate: Rozklady_na_tsili_kvadraty::Rozklady_na_tsili_kvadraty(const Rozklady_na_tsili_kvadraty&)|
    G:\HeloWorld\mn\mn.cpp|35|note: candidate: Rozklady_na_tsili_kvadraty::Rozklady_na_tsili_kvadraty(const Rozklady_na_tsili_kvadraty&, int)|
    G:\HeloWorld\mn\mn.cpp|49|warning: reference to local variable 'tymtsasovyj_ob' returned [-Wreturn-local-addr]|
    G:\HeloWorld\mn\mn.cpp||In member function 'Rozklady_na_tsili_kvadraty& Rozklady_na_tsili_kvadraty::operator+(int)':|
    G:\HeloWorld\mn\mn.cpp|55|warning: reference to local variable 'tymtsasovyj_ob' returned [-Wreturn-local-addr]|
    G:\HeloWorld\mn\mn.cpp||In member function 'int Rozklady_na_tsili_kvadraty::rozdrukuj_chysla()':|
    G:\HeloWorld\mn\mn.cpp|67|warning: no return statement in function returning non-void [-Wreturn-type]|
    G:\HeloWorld\mn\mn.cpp||In function 'int main()':|
    G:\HeloWorld\mn\mn.cpp|83|error: no match for 'operator+=' (operand types are 'Rozklady_na_tsili_kvadraty' and 'int')|
    ||=== Build failed: 2 error(s), 3 warning(s) (0 minute(s), 1 second(s)) ===|

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,069
    Code:
    G:\HeloWorld\mn\mn.cpp|41|note: candidate: Rozklady_na_tsili_kvadraty::Rozklady_na_tsili_kvadraty(const Rozklady_na_tsili_kvadraty&)|
    G:\HeloWorld\mn\mn.cpp|35|note: candidate: Rozklady_na_tsili_kvadraty::Rozklady_na_tsili_kvadraty(const Rozklady_na_tsili_kvadraty&, int)|
    Lemme guess, the second one has a default value.
    If so, then remove the default value from the declaration.

    Also fix all the other warnings as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    the second one has a default value - Why?


    I did not understand what it could be. I, moreover, I do not understand well, how to get out of the function, temporary object.

    Then I will attach the whole project, although I believed that the rest of the code is insignificant.
    Attached Files Attached Files
    Last edited by Dmy; 12-19-2021 at 12:16 PM.

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Constructors in general are only three, and

    Code:
    Rozklady_na_tsili_kvadraty()
        {
            poraxuvaty_tsilyh_dilnykiv=0;
            mI=NULL;
            tymtsasovyj_mI=NULL;
        }
    initial initialization


    Code:
    Rozklady_na_tsili_kvadraty(const Rozklady_na_tsili_kvadraty &ob)
        {
           zminy_znatsennja(ob, I);
        }
    This one is intended for copying an object, and, it means, for an overloaded assignment operator


    Code:
    Rozklady_na_tsili_kvadraty( const Rozklady_na_tsili_kvadraty &ob,  int _I=0)  : I(_I)
        {
           zminy_znatsennja(ob, I);
           kontsentratsija_logiki(I);
        }
    And this one is designed to add a new value .. Naturally, the number of parameters is different.
    Last edited by Dmy; 12-19-2021 at 12:40 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,069
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,402
    Quote Originally Posted by Dmy
    And this one is designed to add a new value .. Naturally, the number of parameters is different.
    The number of parameters is different, but because you provided a default argument, both of them can be called with a single argument of the same type. Thus, it becomes ambiguous which one is intended to be called.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ambiguous overload for operators...
    By Cobs in forum C++ Programming
    Replies: 8
    Last Post: 05-11-2011, 08:44 PM
  2. Overload Operators
    By verbity in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2007, 11:13 AM
  3. using templates to overload operators
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2006, 10:18 PM
  4. How do I Overload these operators in this program?
    By advocation in forum C++ Programming
    Replies: 9
    Last Post: 04-24-2005, 11:29 AM
  5. Replies: 5
    Last Post: 11-24-2002, 11:05 PM

Tags for this Thread