C++ operator precedence

Started by
1 comment, last by sQuid 20 years, 10 months ago
Hi there, I use an overloaded matrix class with overloaded output and multiplication operators

template <typename T> class Matrix {
...
friend ostream& operator<<<>(ostream&, Matrix&;
...
}
 

template <typename T> Matrix<Z> operator*(const Matrix<Z> &, const Matrix<Z> &;
 
Whenever I try to output the result of a multiplication

cout << (A*B) << endl;
 
i get an error

main.cpp:33: could not convert `operator*((&EI))'' to Matrix<cx> &''
matrix.h:215: in passing argument 2 of `operator<< ostream &, Matrix<Z> & [with Z = cx]''
 
What on earth is wrong with this? (gcc v2.95) thanking you ...
Advertisement
Hard to tell with the gcc 2.95 error message. I expect it has to do with operator<< taking a non-const Matrix&.

Try changing the declaration of operator<< to something like
friend ostream& operator<<(ostream&, const Matrix&); 


PS. Also try a newer compiler if possible eg gcc 3.3. Not only is it more standards compliant but a number of the template error messages are much better.

Phil



--
Philip Dunstan
phil@philipdunstan.com
http://www.philipdunstan.com/
--Philip Dunstanphil@philipdunstan.comhttp://www.philipdunstan.com/
Thanks, that worked. I tried that originally but had problems because the << operator was calling my index operator (int,int) which can''t be const.

I''d love to use g++ 3.3. However the sysadmin tells me he doesn''t want to upgrade it because all the old fogies who haven''t got using namespace std; in their code will riot. Or something like that.

This topic is closed to new replies.

Advertisement