Operator overload error

Hi Guys,

I'm at my first experiences with C++. In order to better handle C++ language i've designed a MATRIX class and i've tried to overload the + and = operator.
Matrix.ccp file compile without errors but every time that i try to use a combination of + and = operator i get this error:

main.cpp: In function `int main(int, char**)':

main.cpp:18: error: no match for 'operator=' in 'A = MATRIX::operator+(MATRIX&)(((MATRIX&)(&C)))'
matrix.h:20: note: candidates are: MATRIX& MATRIX::operator=(MATRIX&)

I put down the code. Thanks in advance.

FILE: types.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef __TYPES_H
#define __TYPES_H

typedef char CHAR;

typedef char INT8S;
typedef unsigned char INT8U;

typedef short INT16S;
typedef unsigned short INT16U;

typedef int INT32S;
typedef unsigned int INT32U;

typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;

typedef float REAL32;
typedef double REAL64;

#define pNULL ((void*) 0UL)

#endif 



FILE: matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef __MATRIX_H
#define __MATRIX_H

#include "types.h"

/** \brief Algebraic matrix class
 */
class MATRIX
{
    public:
        MATRIX(INT16U nR = 1, INT16U nC = 1);
        ~MATRIX(void);

        void Print(void);
        INT16U GetRowNum(void);
        INT16U GetClnNum(void);
        void SetElement(INT16U i, INT16U j, REAL32 val);
        REAL32 GetElement(INT16U i, INT16U j);
        REAL32* GetMatrix(void);
        MATRIX& operator=(MATRIX& X);
        MATRIX operator+(MATRIX& X);

    private:
        INT16U nRow;
        INT16U nCln;
        REAL32* M;
};

#endif 


FILE: matrix.ccp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <cstdlib>
#include <iostream>

#include "matrix.h"

using namespace std;

/** \brief Get matrix element
 * \param[in] r Row index
 * \param[in] c Colomn index
 */
#define M(r,c) M[r*nCln + c]

/** \brief MATRIX constructor
 * \param[in] nR Row number
 * \param[in] nC Colomns number
 */
MATRIX::MATRIX(INT16U nR, INT16U nC)
{
    INT16U i = 0, j = 0;

    nRow = nR;
    nCln = nC;
    M = new REAL32[nRow*nCln];

    if(M != pNULL)
    {
        for(i = 0; i < nRow; i++)
        {
            for(j = 0; j < nCln; j++)
            {
                if(i == j)
                {
                    M(i,j) = 1.0F;
                }
                else
                {
                    M(i,j) = 0.0F;
                }
            }
        }
    }
    else
    {
        cout << "\nObject non allcated - Out Of Memory...\n";
    }

}

/** \brief MATRIX destructor
 */
MATRIX:: ~MATRIX(void)
{
    if(M != pNULL)
    {
        delete M;
    }
}

/** \brief Print out matrix components
 */
void MATRIX::Print(void)
{
    INT16U i = 0U, j = 0U;
    if(M != pNULL)
    {
        for(i = 0; i < nRow; i++)
        {
            for(j = 0; j < nCln; j++)
            {
                cout << M(i,j) << " ";
            }
            cout << "\n";
        }
    }
}

/** \brief Get matrix row number
 * \retval Matrix row number
 */
INT16U MATRIX::GetRowNum(void)
{
    return nRow;
}

/** \brief Get matrix Colomns number
 * \retval Matrix row number
 */
INT16U MATRIX::GetClnNum(void)
{
    return nCln;
}

void MATRIX::SetElement(INT16U i, INT16U j, REAL32 val)
{
    M(i,j) = val;
}

REAL32 MATRIX::GetElement(INT16U i, INT16U j)
{
    return (REAL32) M(i,j);
}

REAL32* MATRIX::GetMatrix(void)
{
    return M;
}

MATRIX& MATRIX::operator=(MATRIX& X)
{
    if( (X.GetMatrix() != pNULL) && (&X != this) )
    {
        if( (X.GetClnNum() == nCln) && \
            (X.GetRowNum() == nRow) )
        {
            memcpy((void*) M, (void*)X.GetMatrix(), nCln*nRow*sizeof(REAL32));
        }
    }

    return *this;
}

MATRIX MATRIX::operator+(MATRIX& X)
{
    MATRIX R(nRow, nCln);

    if( (X.GetMatrix() != pNULL) && (&X != this) )
    {
        if( (X.GetClnNum() == nCln) && \
            (X.GetRowNum() == nRow) )
        {
            for(INT16U i = 0; i < nCln; i++)
            {
                for(INT16U j = 0; j < nRow; j++)
                {
                    R.SetElement(i, j, M(i,j) + X.GetElement(i,j));
                }
            }
        }

        return R;
    }
    else
    {
        return *this;
    }
}


FILE: main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdlib>
#include <iostream>

#include "matrix.h"
#include "types.h"

using namespace std;

int main(int argc, char *argv[])
{
    MATRIX A(2,2);
    MATRIX B(2,2);
    MATRIX C(2,2);

    A.Print();
    B.Print();
    C.Print();
    A = (B + C);

    A.Print();
    B.Print();
    C.Print();

    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
1. Please use code tags.
2. The parameter type should be const Matrix&.

I didn't check much more because it hurt my eyes to read unformatted code.
Hi webJose. Sorry, I didn't know about code tags :O . You are right without that format the code is unreadable.
When I change to const qualifier operators parameter i get some other errors reported below. Same code, just parameter changed.

One note: operator= work well if used alone but not in conjunction with operator+.

ERROR after change
matrix.cpp: In member function `MATRIX& MATRIX::operator=(const MATRIX&)':
matrix.cpp:111: error: passing `const MATRIX' as `this' argument of `REAL32* MATRIX::GetMatrix()' discards qualifiers

matrix.cpp:113: error: passing `const MATRIX' as `this' argument of `INT16U MATRIX::GetClnNum()' discards qualifiers
matrix.cpp:113: error: passing `const MATRIX' as `this' argument of `INT16U MATRIX::GetRowNum()' discards qualifiers
matrix.cpp:116: error: passing `const MATRIX' as `this' argument of `REAL32* MATRIX::GetMatrix()' discards qualifiers
matrix.cpp: In member function `MATRIX MATRIX::operator+(const MATRIX&)':
matrix.cpp:127: error: passing `const MATRIX' as `this' argument of `REAL32* MATRIX::GetMatrix()' discards qualifiers
matrix.cpp:129: error: passing `const MATRIX' as `this' argument of `INT16U MATRIX::GetClnNum()' discards qualifiers
matrix.cpp:129: error: passing `const MATRIX' as `this' argument of `INT16U MATRIX::GetRowNum()' discards qualifiers
matrix.cpp:136: error: passing `const MATRIX' as `this' argument of `REAL32 MATRIX::GetElement(INT16U, INT16U)' discards qualifiers
I see the errors. You need to Google up "const correctness" to understand what goes on here. This is also related to your problem. You see, your operator+() returns a temporary copy of a matrix object. ANSI C++ states that temporary copies of objects can only be passed as constant parameters. So the const keyword should satisfy this requirement, but then you must properly qualify your methods.

So read about const correctness and the volatile keyword (you may or may not need it).
WOW! It works out. I can't apply methods to a const objects. removed that also overload operator error disappeared :)
Thx a lot!
Topic archived. No new replies allowed.