好的,我想写一个建立2D矩阵的模板,我希望>>和< <正常工作,这里是我迄今为止的代码,但是,我搞不清楚了。我现在有功能输入和输出以通过填充模板来运行用户,所以我希望能够模板和cout。C++ Template >> and << Overloading trouble
#include <iostream>
#include <cstdlib>
using namespace std;
template <typename T >
class Matrix
{
friend ostream &operator<<(ostream& os,const Matrix& mat);
friend istream &operator>>(istream& is,const Matrix& mat);
private:
int R; // row
int C; // column
T *m; // pointer to T
public:
T &operator()(int r, int c){ return m[r+c*R];}
T &operator()(T a){for(int x=0;x<a.R;x++){
for(int z=0;z<a.C;z++){
m(x,z)=a(x,z);
}
}
}
~Matrix();
Matrix(int R0, int C0){ R=R0; C=C0; m=new T[R*C]; }
void input(){
int temp;
for(int x=0;x<m.R;x++){
for(int y=0;y<m.C;y++){
cout<<x<<","<<y<<"- ";
cin>>temp;
m(x,y)=temp;
}
}
}
};
// istream &operator>>(istream& is,const Matrix& mat){
// is>>mat
// };
ostream &operator<<(ostream& os,const Matrix& mat){
for(int x=0;x<mat.R;x++){
for(int y=0;y<mat.C;y++){
cout<<"("<<x<<","<<y<<")"<<"="<<mat.operator()(x,y);
}
}
};
int main()
{
Matrix<double> a(3,3);
a.input();
Matrix<double> b(a);
cout<<b;
cout << a(1,1);
}
好吧我会添加无效,我只是使用一般指针。我将这些类添加到原始帖子中。 – sdla4ever 2013-05-02 01:38:05
你班上的名字是什么? – 0x499602D2 2013-05-02 01:39:49
class Matrix就是我正在使用的 – sdla4ever 2013-05-02 01:41:13