Thread: Problems: Operator overloading.

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Problems: Operator overloading.

    I'm trying to overload the operators + and +=, however, I'm getting many errors that I have no idea about.. consider the following code:

    Code:
    CMatrix CMatrix::operator+ (const CMatrix &mxToAdd)
    {
    	if (m_pdMatrixData == NULL) return NULL;
    	if (mxToAdd.GetRows() != m_iDimension[0]) return NULL;
    	if (mxToAdd.GetColumns() != m_iDimension[1]) return NULL;
    	// Error Checking;
    
    	CMatrix mxReturnMatrix(m_iDimension[0], m_iDimension[1]);
               
    	for (int i = 0; i < m_iDimension[0]; i++)
    	{
    		for (int ii = 0; ii < m_iDimension[1]; ii++)
    		{
    			double dSetValue = m_pdMatrixData[i][ii] + mxToAdd.GetSingle(i, ii);
    			mxReturnMatrix.SetSingle(i, ii, dSetValue);
    		}
    	}
    	return mxReturnMatrix;
    };
    
    
    void CMatrix::operator+= (const CMatrix &mxToAdd)
    {
    	if (m_pdMatrixData == NULL) return;
    	if (mxToAdd.GetRows() != m_iDimension[0]) return;
    	if (mxToAdd.GetColumns() != m_iDimension[1]) return;
    
    	for (int i = 0; i < m_iDimension[0]; i++)
    	{
    		for (int ii = 0; ii < m_iDimension[1]; ii++)
    		{
    			m_pdMatrixData[i][ii] += mxToAdd.GetSingle(i, ii);
    		}
    	}
    }
    This is the correct syntax, is it not? Here are the errors:

    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(247) : error C2664: '__thiscall CMatrix::CMatrix(const class CMatrix &)' : cannot convert parameter 1 from 'const int' to 'const class CMatrix &'
    Reason: cannot convert from 'const int' to 'const class CMatrix'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(248) : error C2662: 'GetRows' : cannot convert 'this' pointer from 'const class CMatrix' to 'class CMatrix &'
    Conversion loses qualifiers
    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(248) : error C2664: '__thiscall CMatrix::CMatrix(const class CMatrix &)' : cannot convert parameter 1 from 'const int' to 'const class CMatrix &'
    Reason: cannot convert from 'const int' to 'const class CMatrix'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(249) : error C2662: 'GetColumns' : cannot convert 'this' pointer from 'const class CMatrix' to 'class CMatrix &'
    Conversion loses qualifiers
    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(249) : error C2664: '__thiscall CMatrix::CMatrix(const class CMatrix &)' : cannot convert parameter 1 from 'const int' to 'const class CMatrix &'
    Reason: cannot convert from 'const int' to 'const class CMatrix'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(258) : error C2662: 'GetSingle' : cannot convert 'this' pointer from 'const class CMatrix' to 'class CMatrix &'
    Conversion loses qualifiers
    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(269) : error C2662: 'GetRows' : cannot convert 'this' pointer from 'const class CMatrix' to 'class CMatrix &'
    Conversion loses qualifiers
    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(270) : error C2662: 'GetColumns' : cannot convert 'this' pointer from 'const class CMatrix' to 'class CMatrix &'
    Conversion loses qualifiers
    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(276) : error C2662: 'GetSingle' : cannot convert 'this' pointer from 'const class CMatrix' to 'class CMatrix &'
    Conversion loses qualifiers

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You need to make GetRows(), GetColumns(), and GetSingle() const member functions. Or, perhaps, have some methods that are duplicated, one const version, and one non-const version. It may make sense, for example, for a function like GetSingle to return a reference to whatever the CMatrix is of (double, float, int?). The const version would return a const reference, while the non-const version would return a non-const reference, this way, a function using a const CMatrix could access the individual numbers, but not change them, without forcing a copy. On the other hand, a function using a non-const CMatrix could directly change the matrix with GetSingle().

    That should clear up a few errors, but probably not all of them.

    I can't see where in that code you are making a CMatrix with an integer parameter, however, so I don't know what is causing this error.

    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(247) : error C2664: '__thiscall CMatrix::CMatrix(const class CMatrix &)' : cannot convert parameter 1 from 'const int' to 'const class CMatrix &'
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Thanks Silent, that cleared most of the errors up. However, you were right, I still have these

    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(247) : error C2664: '__thiscall CMatrix::CMatrix(const class CMatrix &)' : cannot convert parameter 1 from 'const int' to 'const class CMatrix &'
    Reason: cannot convert from 'const int' to 'const class CMatrix'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(248) : error C2664: '__thiscall CMatrix::CMatrix(const class CMatrix &)' : cannot convert parameter 1 from 'const int' to 'const class CMatrix &'
    Reason: cannot convert from 'const int' to 'const class CMatrix'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\Documents and Settings\Administrator\Desktop\CMatrix.cpp(249) : error C2664: '__thiscall CMatrix::CMatrix(const class CMatrix &)' : cannot convert parameter 1 from 'const int' to 'const class CMatrix &'
    Reason: cannot convert from 'const int' to 'const class CMatrix'
    No constructor could take the source type, or constructor overload resolution was ambiguous

    I have no idea... they point to the following three commented lines:

    Code:
    CMatrix CMatrix::operator+ (const CMatrix &mxToAdd)
    {
    //	if (m_pdMatrixData == NULL) return NULL;
    //	if (mxToAdd.GetRows() != m_iDimension[0]) return NULL;
    //	if (mxToAdd.GetColumns() != m_iDimension[1]) return NULL;
    	
    
    	CMatrix mxReturnMatrix(m_iDimension[0], m_iDimension[1]);
               
    	for (int i = 0; i < m_iDimension[0]; i++)
    	{
    		for (int ii = 0; ii < m_iDimension[1]; ii++)
    		{
    			double dSetValue = m_pdMatrixData[i][ii] + mxToAdd.GetSingle(i, ii);
    			mxReturnMatrix.SetSingle(i, ii, dSetValue);
    		}
    	}
    	return mxReturnMatrix;
    };
    When I commented them, and attempted to compile, I got unresolved external symbols... hmmm.

    Matrix.obj : error LNK2001: unresolved external symbol "public: double __thiscall CMatrix::GetSingle(unsigned short,unsigned short)" (?GetSingle@CMatrix@@QAENGG@Z)
    Matrix.obj : error LNK2001: unresolved external symbol "public: short __thiscall CMatrix::GetColumns(void)" (?GetColumns@CMatrix@@QAEFXZ)
    Matrix.obj : error LNK2001: unresolved external symbol "public: short __thiscall CMatrix::GetRows(void)" (?GetRows@CMatrix@@QAEFXZ)

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What is m_pdMatrixData ?

    What does mxToAdd.GetRows()/mxToAdd.GetColumns() return?

    What is m_iDimension[0]/m_iDimension[1]?

    I suspect you're trying to compare things that aren't comparable, but on the limited code you've posted can't be sure.

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    m_pMatrixData is defined as:
    double **m_pdMatrixData;

    GetRows()/GetColumns() are defined as:

    Code:
    short int  CMatrix::GetRows() const
    {
    	if (m_pdMatrixData == NULL) return 0;
    
    	return m_iDimension[0];
    }
    
    short int CMatrix::GetColumns() const
    {
    	if (m_pdMatrixData == NULL) return 0;
    
    	return m_iDimension[1];
    }
    m_iDimension is defined as:
    unsigned short int m_iDimension[2];

    I'll attach my code aswell, if anyone needs to take a look at any other part of it.

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Sorry, just spotted it. You're returning NULL from operator+ in the cases which you've commented out. This can't be converted to a CMatrix that is supposed to be returned.

  7. #7
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Ahh. What should I return then? If I have a CMatrix that hasn't been filled with any dimensions, how can I return failure?

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You'll have to return a CMatrix in some state that indicates failure, or if you want to complicate things you could return a pointer to a newly allocated CMatrix (in which case you could return NULL for a failure). Or you could throw an exception.

  9. #9
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Alright, I will try that.
    One more problem, well, actually three. The unresolved externals. I have no idea what to do or even look for, I've been up and down the code, whenever I declare my accesor methods as constant, I get these unresolved externals:

    Here are my two files, and the errors are:
    Matrix.obj : error LNK2001: unresolved external symbol "public: double __thiscall CMatrix::GetSingle(unsigned short,unsigned short)" (?GetSingle@CMatrix@@QAENGG@Z)
    Matrix.obj : error LNK2001: unresolved external symbol "public: short __thiscall CMatrix::GetColumns(void)" (?GetColumns@CMatrix@@QAEFXZ)
    Matrix.obj : error LNK2001: unresolved external symbol "public: short __thiscall CMatrix::GetRows(void)" (?GetRows@CMatrix@@QAEFXZ)

    I've stripped it down to just the offending functions.

    Code:
    [CMatrix.cpp]
    
    #include "stdafx.h"
    #include "CMatrix.h"
    #include <cstdarg>
    
    ...
    
    double CMatrix::GetSingle(unsigned short int iRowNum, unsigned short int iColumnNum) const
    {
    	if (m_pdMatrixData == NULL) return NULL;
    	if (iRowNum > m_iDimension[0]) return NULL;
    	if (iRowNum > m_iDimension[1]) return NULL;
    
    	return m_pdMatrixData[iRowNum][iColumnNum];
    }
    
    
    short CMatrix::GetRows() const
    {
    	if (m_pdMatrixData == NULL) return 0;
    
    	return m_iDimension[0];
    }
    
    short CMatrix::GetColumns() const
    {
    	if (m_pdMatrixData == NULL) return 0;
    
    	return m_iDimension[1];
    }
    
    void CMatrix::GetDimensions(short int &iRow, short int &iColumn) const
    {
    	if (m_pdMatrixData == NULL)
    	{
    		iRow = 0;
    		iColumn = 0;
    		return;
    	}
    	iRow = m_iDimension[0];
    	iColumn = m_iDimension[1];
    }
    
    double **CMatrix::GetMatrix() const
    {
    	if (m_pdMatrixData == NULL) return NULL;
    	return m_pdMatrixData;
    }
    
    ...

    Code:
    [CMatrix.h]
    #ifndef MATRIX_H
    #define MATRIX_H
    
    
    class CMatrix {
    
    public:
    
    	...
    
    	double **GetMatrix() const;
    
    	double GetSingle(unsigned short int iRowNum, unsigned short int iColumnNum) const;
    
    	void GetDimensions(short int &iRow, short int &iColumn) const;
    
    	short GetColumns() const;
    
    	short GetRows() const;
    
    	...
    
    private:
    
    
    	...
    	
    	double **m_pdMatrixData;
    
    	double **m_pdTempMatrix;
    
    };
    
    #endif;
    Last edited by Dual-Catfish; 06-16-2002 at 04:39 PM.

  10. #10
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    I hate to bump, but this post seems to have been erased off of the board completly... I can't find it anywhere exept a search. A bug maybe? It should be around the first or second page but it's nowhere to be found.

  11. #11
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I got a little lost towards the end of the thread so its better if you restate the exact problem.

    However, looking at the initial code you posted you may have problems with the operator+ function. I always declare them as friends and return an object of the appropriate class, but you have declared them as member functions. Therefore you may need to return a reference to the CMatrix object.

    Code:
    CMatrix& CMatrix:operator+ (const CMatrix &mxToAdd)
    {
         //addition stuff
         return *this;
    };
    And returning NULL is almost certainly a problem, maybe return a empty matrix or something...

    Anyway just something that stood out to me hope it helps :)

  12. #12
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    I don't quite understand what you mean by declaring them as a friend function. Could you give me an example?

    And what does return *this; do? I've always overloaded my operators this way, it worked before but I'd like to see how you do it.

    Also, I doubt this is going to solve the problem of my unresolved externals (they're not there if I don't declare the member functions as const!)

  13. #13
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The operator + member function can return whatever it wants, but it's best (IE, follows convention for the built in types, +, -, *, /, etc, all create a temporary).

    Do you also have a

    Code:
    short GetColumns() const // non-const
    in your header?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  14. #14
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Yes, you can check my function declarations and definitions in one of my above posts..

    What causes an unresolved external symbol!?

  15. #15
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Bleh.. I meant.

    Code:
    short GetColumns(); // non-const
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM