When modifying a TAD withtemplates to make it generic (add templates):
For input / output data I have this code:
std::ostream& operator<<(std::ostream& os, Matrix & m)
{
os << m.rows() << " " << m.columns() << std::endl;
os << std::setprecision(4) << std::fixed;
for(int i=1; i <= m.rows(); i++)
{
for(int j=1; j <= m.columns(); j++)
{
os << m.value(i,j) << " ";
}
os << std::endl;
}
return os;
}
std::istream& operator>>(std::istream& is, Matrix& m)
{
int rows, columns;
float v;
is >> rows >> columns;
for (int i=1; i<=rows; i++)
{
for (int j=1; j<=columns; j++)
{
is >> v;
m.assign(i,j,v);
}
}
return is;
}
The definition (summary):
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include <stdexcept>
#include <iostream>
#include <iomanip>
template<typename E, int R, int C>
class Matriz
{
public:
Matrix();
private:
E elements_[R][C];
};
#include "matrix.cpp"
#include "matrix_io.cpp"
#endif // MATRIX_HPP
The implementation (summary):
#include <limits>
#include <cmath>
template<typename E, int R, int C>
Matrix<E,R,C>::Matrix()
{
for(int i=0; i<R; i++)
{
for(int j=0; j<C; j++)
{
elements_[i][j] = 0;
}
}
}
Here is the error:
#include "matrix.hpp"
#define Element float
#define MatrixP Matrix<Element, 3, 3>
void tryBuildMatrix()
{
MatrixP m;
std::cout << m;
}
This error:
error: cannot bind 'std::ostream {aka std::basic_ostream}' lvalue to 'std::basic_ostream&&' std::cout << m;
Any idea why it occurs?
PS: if I remove the template the code is perfect.
Aucun commentaire:
Enregistrer un commentaire