hi, i'm having trouble compiling this code:
SparseMatrix.h:
#ifndef SPARSE_MATRIX
#define SPARSE_MATRIX
#include <ostream>
class SparseMatrix {
//class data
...
SparseMatrix operator+(const SparseMatrix& rhs) const;
SparseMatrix operator*(const SparseMatrix& rhs) const;
};
ostream& operator<<(ostream& out, SparseMatrix& rhs);
#endif
SparseMatrix.cpp:
#include "SparseMatrix.h"
ostream& operator<<(ostream& out, SparseMatrix& rhs) {
...
}
SparseMatrix SparseMatrix::operator+(const SparseMatrix& rhs) const {
...
}
SparseMatrix SparseMatrix::operator*(const SparseMatrix& rhs) const {
...
}
and main.cpp:
#include "SparseMatrix.h"
#include <iostream>
int main() {
const SparseMatrix sm(3, 4);
const SparseMatrix sm2(4, 5);
sm.setElement(1, 2, 5);
sm2.setElement(3, 1, 5);
cout << sm << endl;
cout << sm2 << endl;
SparseMatrix m = sm+sm2;
cout << m << endl;
return 0;
}
But the compilation of main.cpp just gives me a long list of STL errors.
main.cpp:7: error: passing ‘const SparseMatrix’ as ‘this’ argument of ‘void SparseMatrix::setElement(int, int, int)’ discards qualifiers
main.cpp:8: error: passing ‘const SparseMatrix’ as ‘this’ argument of ‘void SparseMatrix::setElement(int, int, int)’ discards qualifiers
main.cpp:10: error: no match for ‘operator<<’ in ‘std::cout << sm’
/usr/include/c++/4.4/ostream:108: note: candidates are: bla bla bla
and
SparseMatrix.h:38: note: std::ostream& operator<<(std::ostream&, SparseMatrix&)
main.cpp:11: error: no match for ‘operator<<’ in ‘std::cout << sm2’
/usr/include/c++/4.4/ostream:108: note: candidates are: bla bla bla
What is wrong here?
Thanks!