operator not working.

I would appreciate if someone can tell me what I'm doing wrong with my operatr().

Some of the values are properly being assigned, but other values are then being set to junk like 4064384 or 4063624.

I am posting my full source code because I have a hunch the problems may be resulting from changes I made to my constructors, deconstructors and resize functions.

Thanks again for your help.

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
30
31
32
33
34
35
36
37
38
39
40
#ifndef MATRIX_H
#define MATRIX_H
#include "node.h"
template <class itemType>
class matrix
{
  public:

    matrix( );                                      // default size 0 x 0
    matrix( int rows, int cols );                   // size rows x cols
    matrix( int rows, int cols,
            const itemType & fillValue );           // all entries == fillValue
    matrix( const matrix & mat );                   // copy constructor
    ~matrix( );


  // assignment
    const matrix & operator = ( const matrix & rhs );
    bool operator == (const matrix & rhs) const;
    bool operator != (const matrix & rhs) const;

    int numrows( ) const;                             // number of rows
    int numcols( ) const;                             // number of columns

    const void displaymatrix();
    void resize(int newRows, int newCols);
    itemType & operator ( ) (int k, int y);
    const itemType & operator ( ) (int k, int y) const;
    int myRows;                             // # of rows (capacity)
    int myCols;                             // # of cols (capacity)

    const void clear();

 private:
    NODE<itemType> * mymatrix, * head;
};

#include "matrix.cpp"
#endif


node.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
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
#include <cstdio>
template <class nombre> nombre bigger (const nombre & a, const nombre & b)
{
    if (a >= b)
        return a;
    else return b;
}

#ifndef node_h
#define node_h

template <class item>
struct node {
    node * next;
    item data;

    //node(){ if (next != NULL) { next = new node<item>(0); next == NULL;  }  }
    //node(const bool & b) { }
    node(){ }
    ~node(){ }
};

#endif

#ifndef NODE_H
#define NODE_H

template <class item>
struct NODE {
    NODE * NEXTNODE;
    node<item> * nextnode;
    //NODE(){ nextnode = new node<item>; }
    //NODE(){nextnode = new node<item>; nextnode = NULL; }
    //NODE(){if () }
    //NODE(CONST BOOL & B) { }
    //NODE(const bool first){if (first) { nextnode = new node<item>; NEXTNODE = new NODE<item>; } }

    NODE(){}
    ~NODE(){ } //cout << "This is ~NODE()\n" << endl;  }
};

#endif


#ifndef DEL
#define DEL

template <class type> bool del(type * n)
{
    if (n != NULL)
        {
            type * first; first = n;
            int counter = 0;
            while (n -> next != NULL)
                n = n -> next;
            delete n;
            if (first -> next != NULL)
                del (first);
            else delete first;
        }
    else return false;
}

template <class type> bool del (NODE<type> * N)
{
    if (N != NULL)
        {
            //exit(1);
            NODE<type> * FIRST; FIRST = N;
            //exit(2);
            while (N -> NEXTNODE != NULL)
                {//exit (3);
                    static int counter;
                    cout << "counter = " << ++counter << endl;
                    N = N -> NEXTNODE;
                    //exit(4);
                    }
            exit(5);
            delete N;
            if (FIRST -> NEXTNODE != NULL)
                del (FIRST);
            else delete FIRST;
            return true;
        }
    else return false;
}

#endif 


mainfile:
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
using
namespace
std;

#include <iostream>

#include "matrix.h"

int main()
{
    matrix<int> test(3, 3);
    static int n;
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
            {
                ++n;
                test(i, j) = n;
                cout << "test(i, j) = " << test(i, j) << endl;
            }
    cout << endl << endl;

    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
            {
                cout << test(i, j) << endl;
                ;
            }

    exit(2);
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
            cout << "(i, j) = " << i << " "<< j << " = " << test(i, j) << endl;
            ;
    exit(1);
    test.displaymatrix();


    cout << "You have reached the end of the program part.\n";
    return 0;
}
matrix.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

#ifndef MATRIX_CPP
#define MATRIX_CPP

#include "matrix.h"
#include <stdlib.h>
#include <iostream>
using namespace std;


template <class itemType>
matrix<itemType>::matrix()
        : myRows(0),
          myCols(0)
          //myMatrix(0)
          {
              mymatrix = new NODE<itemType>;
              head = new NODE<itemType>;
              head -> NEXTNODE = mymatrix;
          }

template <class itemType>
matrix<itemType>::matrix(int rows,int cols)
        : myRows(0),
          myCols(0)
          {
    mymatrix = new NODE<itemType>;
    head = new NODE<itemType>;
    mymatrix -> nextnode = new node<itemType>;
    mymatrix -> nextnode -> next = new node<itemType>;
    mymatrix -> nextnode -> next = NULL;
    mymatrix -> NEXTNODE = new NODE<itemType>;
    mymatrix -> NEXTNODE = NULL;
    head -> NEXTNODE = mymatrix;
    resize(3, 3);
}

template <class itemType>
matrix<itemType>::matrix(int rows, int cols, const itemType & fillValue)
        : myRows(rows),
          myCols(cols)

{
    myRows = rows;
    myCols = cols;
    mymatrix = new NODE<itemType>;
    head = new NODE<itemType>;
    head -> NEXTNODE = mymatrix;

    for (int i = 0; i < myRows; ++i)
        for (int j = 0; j < myCols; ++j)
            mymatrix(i, j) = fillValue;
}

template <class itemType>
matrix<itemType>::matrix(const matrix<itemType> & mat)
    : myRows(mat.myRows),
      myCols(mat.myCols)


{
    mymatrix = mat.mymatrix;
    head = mat.head;
    myRows = mat.myRows;
    myCols = mat.myCols;
    for (int i = 0; i < myRows; ++i)
        for (int j = 0; j < myRows; ++j)
            copy(i, j) = mat(i, j);
    mymatrix = mat.mymatrix;
}

template <class itemType>
matrix<itemType>::~matrix ()
{
    cout << "this is ~matrix()\n" << endl;

    delete mymatrix;
    delete head;
    delete mymatrix -> nextnode;
    delete head -> nextnode;
}

template <class itemType>
const matrix<itemType> &
matrix<itemType>::operator = (const matrix<itemType> & rhs)
{
    if (this != &rhs)
    {
        //resize(rhs.myRows)
        //myMatrix.resize(rhs.myRows); 
        myRows = rhs.myRows; 
        myCols = rhs.myCols;
        for (int i = 0; i < myRows; ++i)
            for (int j = 0; j < myCols; ++j)
                mymatrix(i, j) = rhs.mymatrix(i, j);

    }
    return *this;
}

template <class itemType>
bool matrix<itemType>::operator==(const matrix<itemType> &other) const
{
    matrix<itemType> temp(*this);
    bool tv = true;
    if (temp.numrows() != other.numrows())
        tv = false;
    if (temp.numcols() != other.numcols())
        tv = false;
    for (int x = 0; x <temp.numrows(); ++x)
        {
            for (int y = 0; y < temp.numcols(); ++y)
                {
                    if (temp(x, y) != other(x,y))
                        {
                            tv = false;
                        }
                }
        }
    return tv;
}

template <class itemType>
bool matrix<itemType>::operator!=(const matrix<itemType> &other) const
{
    matrix<itemType> temp(*this);
    bool tv;
    tv = (temp == other);
    tv = !tv;
    return tv;
}

template <class itemType>
int matrix<itemType>::numrows() const
{return myRows;}

template <class itemType>
int matrix<itemType>::numcols() const
{return myCols;}

template <class itemType>
void matrix<itemType>::resize(int newRows, int newCols)
{
    int tr = myRows, tc = myCols;
    NODE<itemType> * first; first = new NODE<itemType>; first -> NEXTNODE = mymatrix;

    myRows = newRows;
    myCols = newCols;

    int bigr = bigger(tr, newRows), bigc = bigger(tc, newCols);
    NODE<itemType> * temp; temp = new NODE<itemType>; temp = head -> NEXTNODE;
    for (int i = 0; i < bigr; ++i, temp = temp -> NEXTNODE)
        {
            for (int j = 0; j < bigc; ++j, temp -> nextnode = temp -> nextnode -> next)
                {
                    if (newCols == tc)
                        { break;}
                    if (bigc == tc)
                        {
                            if (j >= newCols)
                                {
                                    if (temp -> nextnode -> next != NULL)
                                        {
                                            del (temp -> nextnode -> next);
                                            break;
                                        }
                                }
                        }
                    else if (bigc == newCols)
                        {
                            if (j >= tc)
                                {
                                    if (temp -> nextnode -> next == NULL)
                                        {
                                            temp -> nextnode = new node<itemType>; temp -> nextnode -> next = new node<itemType>;
                                        }
                                }
                        }
                }
            if (newRows == tr)
                continue;
            if (bigr == tr)
                {
                    if (i >= newRows)
                        {
                            if (temp -> NEXTNODE != NULL)
                                {
                                    del (temp -> NEXTNODE);
                                    break;
                                }
                        }
                }
            else if (bigr == newRows)
                    {
                        if (i >= tr)
                            {
                                if (temp -> NEXTNODE == NULL)
                                    {
                                        temp -> NEXTNODE = new NODE<itemType>;
                                        temp -> NEXTNODE -> nextnode = new node <itemType>;
                                        temp -> NEXTNODE -> nextnode -> next = new node<itemType>;
                                        temp -> NEXTNODE -> nextnode -> next = NULL;
                                    }
                            }
                    }
        }
    mymatrix = temp;
    delete temp -> nextnode;
    delete temp;
    return;
}



template <class itemType>
const void matrix<itemType>::displaymatrix()
{
    matrix<itemType> mat(*this);
    for (int i = 0; i < mat.numrows(); ++i)
        {
            for (int j = 0; j < mat.numcols(); ++j)
                {
                    cout << (i, j) << " ";
                }
            cout << endl;
        }
}



template <class itemType>
const void matrix<itemType>::clear()
{
    for (int i = 0; i < myRows; ++i)
        for (int j = 0; j < myCols; ++j)
            (i, j) = 0;
}

template <class itemType>
itemType & matrix<itemType>::operator ( ) ( int k, int y)
{
    if (k < 0 || myRows <= k)
    {
        cerr << "Illegal row index: " << k << " max index = ";
        cerr << myRows-1 << endl;
        exit(y);
    }
    if (y < 0 || myCols <= y)
        {
            cerr << "Illegal column index: " << y << " max index = " ;
            cerr << myCols-1 << endl;
            exit(k);
        }
    NODE<itemType> * temp = head;
    temp = temp -> NEXTNODE;
    for (int r = 0; r < k; ++r)
        temp = temp -> NEXTNODE;
    for (int c = 0; c < k; ++c)
        {temp -> nextnode = temp -> nextnode -> next; }

    itemType & dat = temp -> nextnode -> data;
    cout << "dat = " << dat << "!!!!!!!!!!!!!!!" << endl;
    delete temp;
    return dat;
}

template <class itemType>
const itemType &
matrix<itemType>::operator ( ) (int k, int y) const

{
    exit(701);
    if (k < 0 || myRows <= k)
    {   cerr << "Illegal row index: " << k << " max index = ";
        cerr << myRows-1 << endl;
        exit(y);
    }
    if (y < 0 || myCols <= y)
        {
            cerr << "Illegal column index: " << y << " max index = ";
            cerr << myCols-1 << endl;
            exit(k);
        }
    NODE<itemType> * temp = head -> NEXTNODE;
    for (int r = 0; r < k; ++r)
        temp = temp -> NEXTNODE;
    for (int c = 0; c < y; ++c)
        temp -> nextnode = temp -> nextnode -> next;
    itemType & rv = temp -> nextnode -> data;;
    return rv;
    return rv;
}

#endif 
Could you explain your data structure? Is your matrix a list of lists?

itemType & matrix<itemType>::operator ( ) ( int k, int y) Why are you deleting temp?
matrix<itemType>::matrix(const matrix<itemType> & mat) What is copy?

1
2
3
4
matrix<itemType>::matrix(int rows, int cols, const itemType & fillValue){
  //...
  mymatrix(i, j) = fillValue; //mymatrix is a NODE pointer, it hasn't the operator()
}


1
2
3
4
5
6
7
8
matrix<itemType>::matrix(int rows,int cols) {
//...
		mymatrix -> nextnode -> next = new node<itemType>;
		mymatrix -> nextnode -> next = NULL; // you just put it to reserve memory, why are you change it to null?
		mymatrix -> NEXTNODE = new NODE<itemType>;
		mymatrix -> NEXTNODE = NULL; // same here
//...
}
Last edited on
In node.h
template <class type> bool del(type * n) I think that you are trying to delete the entire list (or from here to the end). If n != NULL and n->next == NULL you will delete the same pointer twice. Can this happen?
Last edited on
// you just put it to reserve memory, why are you change it to null?

At that point I was desperate, I found that with setting it to NULL my program worked (at least better anyway), whereas before I was getting even more errors.

So i commented out the delete temp line as you suggested, and it seems working better (without the garmage values, but it still crashes later on.)

Am I to take it that when I delete temp, I am actually deleting head too because temp was assigned to head? But If I had assigned a new space in memory for temp with temp = new node<item>, I would have been able to assign temp = head, and then call delete later on temp without inadvertantly deling head?


I also found that by removing line 255 from matrix.cpp: //NODE<itemType> * temp = mymatrix;
it compiles and runs the following test program, but with somewhat unexplained (to me) results:

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
using namespace std;
#include <iostream>
#include <cstdlib>
#include "matrix.h"

int main()
{
    matrix<int> test(3, 3);
    static int n;
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
            {
                ++n;
                test(i, j) = n;
                cout << "(i, j) = " << i << " "<< j << " = " << test(i, j) << endl;
            }
    cout << endl << endl;
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
            cout << "(i, j) = " << i << " "<< j << " = " << test(i, j) << endl;
            ;
    cout << endl << endl;
    test.displaymatrix();


    cout << "You have reached the end of the program part.\n";
    return 0;
}


yields:

(i, j) = 0 0 = 1
(i, j) = 0 1 = 2
(i, j) = 0 2 = 3
(i, j) = 1 0 = 4
(i, j) = 1 1 = 5
(i, j) = 1 2 = 6
(i, j) = 2 0 = 7
(i, j) = 2 1 = 8
(i, j) = 2 2 = 9


(i, j) = 0 0 = 3
(i, j) = 0 1 = 3
(i, j) = 0 2 = 3
(i, j) = 1 0 = 6
(i, j) = 1 1 = 6
(i, j) = 1 2 = 6
(i, j) = 2 0 = 9
(i, j) = 2 1 = 9
(i, j) = 2 2 = 9


0 1 2
0 1 2
0 1 2
this is ~matrix()

You have reached the end of the program part.
this is ~matrix()


Process returned 0 (0x0)   execution time : 0.047 s
Press any key to continue.



so which of the 3 values does my matrix actually contain?
Last edited on
Topic archived. No new replies allowed.