Dev C++

Discussion in 'C++' started by Crazy c, Apr 20, 2008.

  1. Crazy c

    Crazy c Guest

    I have hit an error that I do not understand. That is one thing about
    Dev that I do not like, I cannot understand the errors. There should
    be a course in Dev errors. It is pretty straightforward; I am checking
    that the real and imginary parts of a Complex Number are not equal to
    0 (division). In (a,b)/(c,d), if c && d = 0; I want to display an
    error message and return to the program.



    ComplexNumber &ComplexNumber :: operator/ (const ComplexNumber & c1)
    {
    //check for c & d = 0
    if (c1.getReal()== 0 && c1.getImag()== 0) {
    cout << "The Complex Number used for your
    denominator cannot contain 0 for the real AND
    the imaginary part" << endl;
    return 1;// this generates the error
    }

    ComplexNumber complxSum;
    double s;
    //for s
    s = ((this -> getImag() * this -> getImag()) +
    (c1.getImag() * c1.getImag()));
    //divide
    complxSum.setReal ( ((c1.getReal() * this -> getReal())
    + (c1.getImag() * this -> getImag())) /s);
    complxSum.setImag ( ((c1.getReal() * this -> getImag())
    - (c1.getImag() * this -> getReal())) /s);

    return complxSum;
    }//end operator/


    //error from Dev
    102 C:\Documents and Settings\cbrown\My Documents\DSCmplxNum
    \ComplexNumber.cpp invalid initialization of non-const reference of
    type 'ComplexNumber&' from a temporary of type 'int'

    Any ideas as to why the return does not work or what the error
    designates?

    Thanks!!

    PS - This is the first time I've posted code, so please go easy on
    pointing out mistakes
     
    Crazy c, Apr 20, 2008
    #1
    1. Advertising

  2. On 2008-04-20 18:50, Crazy c wrote:
    > I have hit an error that I do not understand. That is one thing about
    > Dev that I do not like, I cannot understand the errors. There should
    > be a course in Dev errors. It is pretty straightforward; I am checking
    > that the real and imginary parts of a Complex Number are not equal to
    > 0 (division). In (a,b)/(c,d), if c && d = 0; I want to display an
    > error message and return to the program.
    >
    >
    >
    > ComplexNumber &ComplexNumber :: operator/ (const ComplexNumber & c1)
    > {
    > //check for c & d = 0
    > if (c1.getReal()== 0 && c1.getImag()== 0) {
    > cout << "The Complex Number used for your
    > denominator cannot contain 0 for the real AND
    > the imaginary part" << endl;
    > return 1;// this generates the error
    > }
    >
    > ComplexNumber complxSum;
    > double s;
    > //for s
    > s = ((this -> getImag() * this -> getImag()) +
    > (c1.getImag() * c1.getImag()));
    > //divide
    > complxSum.setReal ( ((c1.getReal() * this -> getReal())
    > + (c1.getImag() * this -> getImag())) /s);
    > complxSum.setImag ( ((c1.getReal() * this -> getImag())
    > - (c1.getImag() * this -> getReal())) /s);
    >
    > return complxSum;
    > }//end operator/
    >
    >
    > //error from Dev
    > 102 C:\Documents and Settings\cbrown\My Documents\DSCmplxNum
    > \ComplexNumber.cpp invalid initialization of non-const reference of
    > type 'ComplexNumber&' from a temporary of type 'int'
    >
    > Any ideas as to why the return does not work or what the error
    > designates?


    As the error message states the / operator returns a reference to a
    ComplexNumber object. However you return 1, which will be used to
    construct a ComplexNumber object (with the value 1+0i, I would assume).
    This object will be a temporary, and you can not bind a reference to a
    temporary (you can bind const references to temporaries though).

    In your case you do not want to return a reference to a ComplexNumber,
    you want to return a copy, because when operator/ returns the object you
    created to store the result in will be destructed, and any references to
    it will be invalid (trying to use such a reference will result in UB
    (undefined behaviour), which will likely crash your program).

    Also, returning a value that indicates that the operation went wrong is
    a bad idea, how will you tell the difference between an operation where
    the result is 1+0i and an operation that went wrong? What you want to do
    in this situation is to throw an exception, that is what they are for.

    > PS - This is the first time I've posted code, so please go easy on
    > pointing out mistakes


    You are doing just fine, but in the future you might want to consider
    changing the code so that a tab is represented by somewhere 2 and 4
    spaces, it reduces the risk of line-wrapping.

    --
    Erik Wikström
     
    Erik Wikström, Apr 20, 2008
    #2
    1. Advertising

  3. On 2008-04-20 19:59, Sam wrote:
    > Crazy c writes:
    >
    >> I have hit an error that I do not understand. That is one thing about
    >> Dev that I do not like, I cannot understand the errors. There should
    >> be a course in Dev errors. It is pretty straightforward; I am checking
    >> that the real and imginary parts of a Complex Number are not equal to
    >> 0 (division). In (a,b)/(c,d), if c && d = 0; I want to display an
    >> error message and return to the program.
    >>
    >>
    >>
    >> ComplexNumber &ComplexNumber :: operator/ (const ComplexNumber & c1)
    >> {
    >> //check for c & d = 0
    >> if (c1.getReal()== 0 && c1.getImag()== 0) {
    >> cout << "The Complex Number used for your
    >> denominator cannot contain 0 for the real AND
    >> the imaginary part" << endl;
    >> return 1;// this generates the error
    >> }
    >>
    >> ComplexNumber complxSum;
    >> double s;
    >> //for s
    >> s = ((this -> getImag() * this -> getImag()) +
    >> (c1.getImag() * c1.getImag()));
    >> //divide
    >> complxSum.setReal ( ((c1.getReal() * this -> getReal())
    >> + (c1.getImag() * this -> getImag())) /s);
    >> complxSum.setImag ( ((c1.getReal() * this -> getImag())
    >> - (c1.getImag() * this -> getReal())) /s);
    >>
    >> return complxSum;
    >> }//end operator/

    > w>
    >>
    >> //error from Dev
    >> 102 C:\Documents and Settings\cbrown\My Documents\DSCmplxNum
    >> \ComplexNumber.cpp invalid initialization of non-const reference of
    >> type 'ComplexNumber&' from a temporary of type 'int'
    >>
    >> Any ideas as to why the return does not work or what the error
    >> designates?

    >
    > Your function returns a reference to an object. Instead of returning a
    > reference to an object, your code tries to return an int value. The compiler
    > is telling you that this is not a valid type conversion.
    >
    > If your ComplexNumber object has a constructor that takes an int parameter,
    > just change the function to return a ComplexNumber object, rather than a
    > reference to a ComplexNumber object.


    A constructor have no return-type, so you can't change it.

    --
    Erik Wikström
     
    Erik Wikström, Apr 20, 2008
    #3
  4. On 2008-04-20 20:22, Erik Wikström wrote:
    > On 2008-04-20 18:50, Crazy c wrote:
    >> I have hit an error that I do not understand. That is one thing about
    >> Dev that I do not like, I cannot understand the errors. There should
    >> be a course in Dev errors. It is pretty straightforward; I am checking
    >> that the real and imginary parts of a Complex Number are not equal to
    >> 0 (division). In (a,b)/(c,d), if c && d = 0; I want to display an
    >> error message and return to the program.
    >>
    >>
    >>
    >> ComplexNumber &ComplexNumber :: operator/ (const ComplexNumber & c1)
    >> {
    >> //check for c & d = 0
    >> if (c1.getReal()== 0 && c1.getImag()== 0) {
    >> cout << "The Complex Number used for your
    >> denominator cannot contain 0 for the real AND
    >> the imaginary part" << endl;
    >> return 1;// this generates the error
    >> }
    >>
    >> ComplexNumber complxSum;
    >> double s;
    >> //for s
    >> s = ((this -> getImag() * this -> getImag()) +
    >> (c1.getImag() * c1.getImag()));
    >> //divide
    >> complxSum.setReal ( ((c1.getReal() * this -> getReal())
    >> + (c1.getImag() * this -> getImag())) /s);
    >> complxSum.setImag ( ((c1.getReal() * this -> getImag())
    >> - (c1.getImag() * this -> getReal())) /s);
    >>
    >> return complxSum;
    >> }//end operator/
    >>
    >>
    >> //error from Dev
    >> 102 C:\Documents and Settings\cbrown\My Documents\DSCmplxNum
    >> \ComplexNumber.cpp invalid initialization of non-const reference of
    >> type 'ComplexNumber&' from a temporary of type 'int'
    >>
    >> Any ideas as to why the return does not work or what the error
    >> designates?

    >
    > As the error message states the / operator returns a reference to a
    > ComplexNumber object. However you return 1, which will be used to
    > construct a ComplexNumber object (with the value 1+0i, I would assume).
    > This object will be a temporary, and you can not bind a reference to a
    > temporary (you can bind const references to temporaries though).


    Actually, I'm assuming a bit to much here, as Sam pointed out, you are
    trying to return an int when a reference to a ComplexNumber is expected
    which does not work. Anyway, the solution is still the same.

    --
    Erik Wikström
     
    Erik Wikström, Apr 20, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Peterson

    /dev/urandom vs. /dev/random

    Ron Peterson, Jan 7, 2005, in forum: C Programming
    Replies:
    21
    Views:
    1,583
    Keith Thompson
    Jan 13, 2005
  2. AC
    Replies:
    0
    Views:
    186
  3. Guest
    Replies:
    2
    Views:
    163
    Bob Barrows [MVP]
    Sep 18, 2005
  4. Eirikur Hallgrimsson
    Replies:
    3
    Views:
    175
    Gavin Sinclair
    Oct 10, 2004
  5. Michele Dondi
    Replies:
    1
    Views:
    162
    Anno Siegel
    Feb 22, 2004
Loading...

Share This Page