CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2004
    Posts
    60

    help with a snippet

    Alright, i have starred at this for sometime, and while i could try every combination of const modifier, I would rather get an explanation why this won't go. I still get hung up on const stuff...

    Code:
    Matrix& Matrix::operator = (const Matrix& m) {
    	if (*this == m) {
    		for (int r = 0; r < rows(); r++) {
    			for (int c = 0; c < cols(); c++) {
    				(*this)(r,c) = m(r,c);
    			}
    		}
    		return *this;
    	} else {
    		throw std::invalid_argument("Error! Matricies must be equal for assignment.");
    	}
    }
    I get the following error on compile:

    error C2662: 'cols' : cannot convert 'this' pointer from 'const class Matrix' to 'class Matrix &'
    Conversion loses qualifiers
    error C2662: 'rows' : cannot convert 'this' pointer from 'const class Matrix' to 'class Matrix &'
    Conversion loses qualifiers
    Thanks in advance!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    The error refers to cols() and rows(), but you didn't post what these are. I get no errors with the following code:
    Code:
    class Matrix
    {
      public:
           int nRows;
           int nCols;
           int rows() const { return nRows; }
           int cols() const { return nCols; }
      
           Matrix& operator = (const Matrix& m);
    };
    
    Matrix& Matrix::operator = (const Matrix& m)
    {
        int r = rows();
        return *this;
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jun 2004
    Posts
    60

    maybe that's it...

    I apologize for the incompleteness of my problem, but I thought it had to do with something i did wrong in that function. Here were the original definitions for that class (exactly as you had guessed):

    in header file:
    Code:
    public:
    int rows() {return m_nRows;};
    int cols() {return m_nCols;};
    
    private:
    int m_nRows, m_nCols;
    So now i have modifed rows() and cols() to match yours and everything works perfectly. So correct me if I am wrong, but placing the const in rows() and cols() function makes what rows() and cols() pass back const?

    Thanks for the assisstance!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured