Using overloaded iterators inside class functions

Hi;

I have the following simple 2d array class:

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
/*
 *  array2d.h 
 *
 *  Created on 04/04/09
 *
 *  18/12/2010: added swap function, another operator* to deal with array*double
 *              and a ld& operator() to deal with array(1,2) = 2.0; cout << array(2,3);  
 *
 */

//Class prototype, constructor and destructor for square matrices
//Based on slides from Mr. McQuain at:
//http://courses.cs.vt.edu/~cs2704/spring00/mcquain/Notes/Managing2DArrays.pdf

//Need this include for setw() function when printing array
#include <iomanip>


//Rename types to make code more readable 
typedef long double ld;


class array2d 
{
private:
	ld * data;
	//Side length of square matrix
	int l;
	int i, j, k;
	//Check if input is within range
	bool validRC(int r, int c) const;
	//Check size of outside matrix used in operators
	bool checkSize(const array2d& m) const;
public:
	//Default constructor and destructor
	array2d();
	~array2d();
	//Allocate 1D array of r*c cells
	array2d(int length);
	
	//Get value from 2d array
	ld get(int r, int c) const;
	//Write value in 2d array
	void set(int r, int c, ld value);
	//Get size of matrix
	int getSize() const;
	
	//Matrix operators
	ld&     operator()(int r, int c);
	void    operator=(const array2d& m);
	array2d operator*(const double n);
	array2d operator*(const array2d& m);
};

array2d::array2d(int length) 
{
	l = length;

	data = new ld[l*l];
	if (data == NULL) {
		cerr << "Cannot allocate space for matrix" << endl;
		l = 0;
	}
	//Initializing array with 0 entries
	for (i=0;i < l*l; i++)
		data[i] = 0.0;
}

array2d::~array2d() 
{
	delete [] data;
}

bool array2d::validRC(int r, int c) const 
{
	if ((r > -1) && (r < l) && (c > -1) && (c < l))
		return 1;
	else {
		cerr << "Trying to read/write matrix with row and col values outsite range" << endl;
		return 0;
	}
}

bool array2d::checkSize(const array2d& m) const
{
	if (l == m.getSize()) return 1;
	else {
		cerr << "Cannot carry out operation with different size matrices" << endl;
		return 0;
	}
}

int array2d::getSize() const
{
	return l;
}

ld array2d::get(int r, int c) const 
{
	return data[r*l+c];
}

void array2d::set(int r, int c, ld value) 
{
	data[r*l+c] = value;
}


//Matrix operators
//Get and set value
//Notice array entry is passed by reference (ld&)
ld& array2d::operator()(int r, int c)
{
	if (validRC(r,c)) return data[r*l+c];
}

void array2d::operator=(const array2d& m) 
{
	if (checkSize(m)) {
		for (i=0; i < l; i++) {
			for (j=0; j < l; j++)
				set(i,j, m.get(i,j));
		}
	}
}

//Writing 2 * operators here: one for multiplication with double
//and another for multiplication with other array
//Multiplication with double multiplies each entry by the double
//like in MATLAB
array2d array2d::operator*(const double n) 
{
	array2d holder(l);
	
	for (i=0; i < l; i++) {
		for (j=0; j < l; j++) {
			holder.set(i,j, n*get(i,j));
		}
	}
	return holder;
}

array2d array2d::operator*(const array2d& m) 
{
	array2d holder(l);
	
	if (checkSize(m)) {
		for (i=0; i < l; i++) {
			for (j=0; j < l; j++) {
				for (k=0; k < l; k++)
					holder.set(i,j, holder.get(i,j) + get(i,k)*m.get(k,j));
			}
		}
	}
	return holder;
}



I would like to use the overloaded () operator inside the class functions like:

1
2
3
4
5
6
7
8
9
void array2d::operator=(const array2d& m) 
{
	if (checkSize(m)) {
		for (i=0; i < l; i++) {
			for (j=0; j < l; j++)
				(this object)(i,j) = m(i,j);
		}
	}
}



Any help would be greatly appreciated

Alex
Last edited on
this->operator()(i,j) or (*this)(i,j)
Great thanks I actually tried *this but I didn't put the brackets!
After I changed everything to (*this) and compiled main() with the following:

1
2
3
4
5
6
 
double delta = 0.001;
array2d kspace(16);

kspace = kspace*delta*delta; 


I got this weird error:
circleFFTw.cpp:106: error: no match for ‘operator=’ in ‘kspace = array2d::operator*(double)(delta)’
array2d.h:175: note: candidates are: void array2d::operator=(array2d&)

Do you know what the reason for this might be?


Thank you for your time

Alex



Last edited on
change this -> void array2d::operator=(array2d &)

into this -> void array2d::operator=(const array2d &)
Last edited on
Hi, thanks it worked but I'm not sure why

Isn't const only used so that the compiler tells you if you change the value of the type with const in front?
Yes, that's what const is used for. The thing is that your array2d::operator*(double) returns
a temporary array2d object and temporary objects can't be bound to non-const references.
Ok, I get it. So by temporary objects you mean the ones created if you have:

object a
object b
object c

c = a*b

Then the result of a*b is a temporary object?

Last edited on
I got it, thanks
Topic archived. No new replies allowed.