Question regarding template

Hello all, why the codes below is wrong in g++ 3.4.2? an error occurs in the line: cout<<CMatrix<double>::eye(5); -------------------------------------- #include <ostream> #include <boost/numeric/ublas/matrix.hpp> #include <boost/numeric/ublas/io.hpp> using namespace boost::numeric::ublas; using namespace std; template <class T> class CMatrix { matrix<T>mat_; public: CMatrix(const identity_matrix<T>& m) { mat_ = m; } static CMatrix<T> eye(int size) { return identity_matrix<T>(size); } template <class T_> friend ostream& operator << (ostream& os, CMatrix<T_>& m); }; template<class T_> ostream& operator << (ostream& os, CMatrix<T_>& m) { os<<m.mat_; return os; } int main() { CMatrix<double>I = CMatrix<double>::eye(5); cout<<I; cout<<CMatrix<double>::eye(5);//this line ==> error } ---------------------------------------- Thanks in advance!

On 10/15/06, YYW <yyw1_25@sohu.com> wrote:
Hello all, why the codes below is wrong in g++ 3.4.2? an error occurs in the line: cout<<CMatrix<double>::eye(5);
Whats the error? Kobi. --------------------------------------
#include <ostream> #include <boost/numeric/ublas/matrix.hpp> #include <boost/numeric/ublas/io.hpp>
using namespace boost::numeric::ublas; using namespace std;
template <class T> class CMatrix { matrix<T>mat_;
public: CMatrix(const identity_matrix<T>& m) { mat_ = m; } static CMatrix<T> eye(int size) { return identity_matrix<T>(size); }
template <class T_> friend ostream& operator << (ostream& os, CMatrix<T_>& m); };
template<class T_> ostream& operator << (ostream& os, CMatrix<T_>& m) { os<<m.mat_; return os; }
int main() { CMatrix<double>I = CMatrix<double>::eye(5); cout<<I;
cout<<CMatrix<double>::eye(5);//this line ==> error } ---------------------------------------- Thanks in advance!
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- - Kobi

"YYW" <yyw1_25@sohu.com> wrote
why the codes below is wrong in g++ 3.4.2? an error occurs in the line: cout<<CMatrix<double>::eye(5); -------------------------------------- #include <ostream> #include <boost/numeric/ublas/matrix.hpp> #include <boost/numeric/ublas/io.hpp>
using namespace boost::numeric::ublas; using namespace std;
template <class T> class CMatrix { matrix<T>mat_;
public: CMatrix(const identity_matrix<T>& m) { mat_ = m; } static CMatrix<T> eye(int size) { return identity_matrix<T>(size); }
template <class T_> friend ostream& operator << (ostream& os, CMatrix<T_>& m); };
template<class T_> ostream& operator << (ostream& os, CMatrix<T_>& m)
Try this: ostream& operator << (ostream& os, const CMatrix<T_>& m) Regards, Arkadiy

template <class T> class CMatrix{ matrix<T>mat_; public: CMatrix(const identity_matrix<T>& m) { mat_ = m; } static CMatrix<T> eye(int size) { return identity_matrix<T>(size); } }; template<class T> void operator << (ostream& os, CMatrix<T>& m)//////////// line A {} int main(){ CMatrix<double> I = CMatrix<double>::eye(5); cout<<I; cout<<CMatrix<double>::eye(5);//////////////// line B } ------------------------------------------- In the codes above, "line B" will causes an error in g++ 3.4.2: no match for 'operator<<' in 'std::cout << CMatrix<T>::eye(int) [with T = double]()' Arkadiy Vertleyb advised me to insert "const" in "line A". So, the new version of "line A" is: void operator << (ostream& os, const CMatrix<T>& m) OK, there's no error. But, why? "Arkadiy Vertleyb" <vertleyb@hotmail.com> ??????:eh0dec$ih5$1@sea.gmane.org...
"YYW" <yyw1_25@sohu.com> wrote
why the codes below is wrong in g++ 3.4.2? an error occurs in the line: cout<<CMatrix<double>::eye(5); -------------------------------------- #include <ostream> #include <boost/numeric/ublas/matrix.hpp> #include <boost/numeric/ublas/io.hpp>
using namespace boost::numeric::ublas; using namespace std;
template <class T> class CMatrix { matrix<T>mat_;
public: CMatrix(const identity_matrix<T>& m) { mat_ = m; } static CMatrix<T> eye(int size) { return identity_matrix<T>(size); }
template <class T_> friend ostream& operator << (ostream& os, CMatrix<T_>& m); };
template<class T_> ostream& operator << (ostream& os, CMatrix<T_>& m)
Try this:
ostream& operator << (ostream& os, const CMatrix<T_>& m)
Regards, Arkadiy

"YYW" <yyw1_25@sohu.com> wrote
Arkadiy Vertleyb advised me to insert "const" in "line A". So, the new version of "line A" is: void operator << (ostream& os, const CMatrix<T>& m)
OK, there's no error. But, why?
Because:
static CMatrix<T> eye(int size) { return identity_matrix<T>(size); }
Returns an r-value, and...
template <class T_> friend ostream& operator << (ostream& os, CMatrix<T_>& m); };
you are trying to bind this r-value to a non-const reference. Compiler correctly refuses to do this. IOW, compiler tries to prevent you from changing the temporary object since such changes wouldn't make any sence. HTH, Arkadiy
participants (3)
-
Arkadiy Vertleyb
-
Kobi Cohen-Arazi
-
YYW