Thread: passing discards qualifiers? overloading =...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    passing discards qualifiers? overloading =...

    I'm _attempting_ to overload the assignment operator so I can safely set one object equal to another.

    Here's my code for the operator overloading:

    Code:
    const Matrix Matrix::operator=(const Matrix &temp)
    {
      if(this == &temp)
        return *this; // to prevent bad things from happening..
    
      int x;
      int y;
      int r = temp.numR();
      int c = temp.numC();
      
      for(x = 0; x < r; x++)
      {
        for(y = 0; y < c; y++)
        {
          Array[x][y] = temp.Array[x][y];
        }
      }
      return *this;
    }
    I get the following error:

    matrix.cpp: In member function `const Matrix Matrix:perator=(const Matrix&)':
    matrix.cpp:307: passing `const Matrix' as `this' argument of `int Matrix::numR()' discards qualifiers
    matrix.cpp:308: passing `const Matrix' as `this' argument of `int Matrix::numC()' discards qualifiers

    All of the examples of overloading = say that the declaration should be like so:

    const Matrix &Matrix:perator=(const Matrix &temp)

    but that further complicates my program, since I don't declare my objects on the heap. What to do about that if it really matters?

    And here is the rest of the code for reference:

    http://dydx.no-ip.com:8080/matrix.cpp
    http://dydx.no-ip.com:8080/matrix.h

    Thanks all!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    "return *this" returns the object. use "this" instead. Anyway you could do it just as well without returning anything:

    Code:
    void Matrix:: operator=(const Matrix &temp)
    {
      int x;
      int y;
      int r = temp.numR();
      int c = temp.numC();
      
      for(x = 0; x < r; x++)
      {
        for(y = 0; y < c; y++)
        {
          Array[x][y] = temp.Array[x][y];
        }
      }  
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Declare numR and numC as constant functions.
    Code:
    int numR() const {return a;}
    int numC() const {return b;}
    Then it will work
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    the assignment operator should return a non-const reference not an object.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    well I redefined it as so:

    Code:
    Matrix &Matrix::operator=(const Matrix &temp)
    {
      if(this == &temp)
        return *this; // to prevent bad things from happening..
    
      int x;
      int y;
      int r = temp.numR();
      int c = temp.numC();
      
      for(x = 0; x < r; x++)
      {
        for(y = 0; y < c; y++)
        {
          Array[x][y] = temp.Array[x][y];
        }
      }
      return *this;
    }
    and I set numR and numC to const
    But it causes the program to bog the system down to the point of me having to reboot.

    Any ideas?

    Thanks!

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Cant see a problem with it. How bigs the array?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Stoned_Coder
    Cant see a problem with it. How bigs the array?
    8x8 float array.

    check the files in my original post.

    I'm using the = operator to set 2 matrix objects equal to each other - neither are pointers, they are just objects.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    I just tried this:

    Code:
    Matrix Matrix::operator=(Matrix temp)
    {
      int x;
      int y;
      int r = temp.numR();
      int c = temp.numC();
      
      for(x = 0; x < r; x++)
      {
        for(y = 0; y < c; y++)
        {
          Array[x][y] = temp.Array[x][y];
        }
      }
      return *this;
    }
    Which seems like it should work, but it still doesn't. it compiles but then crashes when running.

    The way I understand it is it takes a Matrix as input (the object on the right of the =) and sets the object on the left of the = (this) equal to it, part by part. Then it returns the object, *this.

    Whats going on here?

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Another variant, same result:

    Code:
    Matrix Matrix::operator=(const Matrix &in)
    {
      int x;
      int y;
      int r = in.numR();
      int c = in.numC();
    
      Matrix temp(r, c);
      
      for(x = 0; x < r; x++)
      {
        for(y = 0; y < c; y++)
        {
          temp.Array[x][y] = in.Array[x][y];
        }
      }
      return temp;
    }

    There must be a problem somewhere else...

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    FINALLY got it to work:

    Code:
    void Matrix::operator=(const Matrix &temp)
    {
      int x;
      int y;
      int r = temp.numR();
      int c = temp.numC();
    
      this->setR(r);
      this->setC(c);
      
      for(x = 0; x < r; x++)
      {
        for(y = 0; y < c; y++)
        {
          Array[x][y] = temp.Array[x][y];
        }
      }
    }
    And in Inverse() I had to do

    temp = *this;

    Anyway, it works as far as I can see. PAIN IN THE ASS getting there, but I learned some stuff along the way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. discards qualifiers
    By EstateMatt in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2009, 03:05 PM
  2. Replies: 8
    Last Post: 05-29-2008, 12:47 PM
  3. Replies: 1
    Last Post: 03-31-2008, 05:53 PM
  4. error: passing [...] discards qualifiers
    By carlorfeo in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 08:45 AM
  5. Replies: 2
    Last Post: 01-04-2003, 03:35 AM