Problems with overloaded operators

Hello everybody,

I have a problem with the use of overloaded operators for a self-defined class "SparseMatrix". Here first the class "SparseMatrix":
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
class SparseMatrix{
    
    int M,N;
public:
    std::map<std::pair<int,int>, double> data;
    
    SparseMatrix(const int n);

    SparseMatrix(const int m, const int n);

    double & 
    operator()(int m,int n);

    int numCols() const;

    int numRows() const;

};



SparseMatrix::SparseMatrix(const int n):M(n),N(n){}


SparseMatrix::SparseMatrix(const int m, const int n):M(m),N(n){}



//this operator is used to set the non-zero values in the sparse-matrix
//CAREFUL: Do not try to access matrix positions where the value is zero, since this creates an entry with value zero!
// Then SparseMatrix is not sparse anymore!
double &
SparseMatrix::operator()(int m, int n){ 
    return data[std::pair<int,int>(m,n)];
}


int
SparseMatrix::numRows() const{
    return M;
}

int 
SparseMatrix::numCols() const{
    return N;
}


std::ostream& 
operator<< (std::ostream& out, SparseMatrix & matrix){
    for(std::map<std::pair<int,int>,double>::iterator iterator = matrix.data.begin(); iterator != matrix.data.end(); iterator++) {
        out << "(" << (iterator->first).first << "," << (iterator->first).second << ") => " << iterator->second << std::endl;
    }
    return out;
}

#endif 


And here my overloaded operators for a multiplication of a "double" wiht a object from "SparseMatrix", a summation of two obejects from "SparseMatrix" and a subtraction of two objects from "SparseMatrix":
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
SparseMatrix
operator*(const double scalar, SparseMatrix &m){
    SparseMatrix ret(m.numRows());
    for(int k = 0; k < ret.numRows(); k++) {
        if(k < (ret.numRows()-1)){
			ret(k,k) = scalar*m(k,k);
			ret(k,k+1) = scalar*m(k,k+1);
			ret(k+1,k) = scalar*m(k+1,k);
		}
		else{
			ret(k,k) = scalar*m(k,k);
		}
    }
    return ret;
}

SparseMatrix
operator+(SparseMatrix &m, SparseMatrix &A){
	SparseMatrix ret(m.numRows());
	for(int k = 0; k < ret.numRows(); k++) {
        if(k < (ret.numRows()-1)){
			ret(k,k) = m(k,k)+A(k,k);
			ret(k,k+1) = m(k,k+1)+A(k,k+1);
			ret(k+1,k) = m(k+1,k)+A(k+1,k);
		}
		else{
			ret(k,k) = m(k,k)+A(k,k);
		}
    }
    return ret;
}

SparseMatrix
operator-(SparseMatrix &m, SparseMatrix &A){
	SparseMatrix ret(m.numRows());
	for(int k = 0; k < ret.numRows(); k++) {
        if(k < (ret.numRows()-1)){
			ret(k,k) = m(k,k)-A(k,k);
			ret(k,k+1) = m(k,k+1)-A(k,k+1);
			ret(k+1,k) = m(k+1,k)-A(k+1,k);
		}
		else{
			ret(k,k) = m(k,k)-A(k,k);
		}
    }
    return ret;
}

When I use these operators for example like this way ("first scalar times SparseMatrix, then SparseMatrix minus SparseMatrix")

(M-((1-theta)*T/timesteps)*A)

where M, A are of the type "SparseMatrix" and theta, T, timesteps are of the type "double", I get the following error:

simpleparabolicfem1d.h:189: error: no match for 'operator-' in '((SimpleParabolicFEM1D*)this)->SimpleParabolicFEM1D::M - operator*(double, SparseMatrix&)(((SparseMatrix&)(&((SimpleParabolicFEM1D*)this)->SimpleParabolicFEM1D::A)))'
operators.h:121: note: candidates are: SparseMatrix operator-(SparseMatrix&, SparseMatrix&)


How I get rid of this error? Thanks for your help!
SparseMatrix operator-(const SparseMatrix &m, const SparseMatrix &A){

Or you may not use temporaries as arguments to the operator.
Last edited on
But when I use

SparseMatrix operator-(const SparseMatrix &m, const SparseMatrix &A){


I get problems with my "SparseMatrix" operator

1
2
double & 
operator()(int m,int n);


And when I change this operator also to const:

1
2
double & 
operator()(int m,int n) const;


I get the following trouble

passing 'const std::map<std::pair<int, int>, double>' as 'this' argument of 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<int, int>, _Tp = double, _Compare = std::less<std::pair<int, int> >, _Alloc = std::allocator<std::pair<const std::pair<int, int>, double> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = double, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<int, int>]' discards qualifiers [-fpermissive]


What can I do? How can I solve the problems with the operators? Are there any suggestions?
double& operator()(int m,int n);

> when I change this operator also to const

Do not change; overload. http://www.parashift.com/c++-faq/const-overloading.html

1
2
3
4
5
double& operator()(int m,int n);

double& operator()(int m,int n) const ;

const double& operator()(int m,int n) const ;
Topic archived. No new replies allowed.