如果是用VS2005編譯的話,會找不到<iostream.h>,應該用<iostream>,但是VC6.0應該沒問題。
修改後至少能編譯過的代碼:(VS2005下編譯通過)
#include <stdlib.h >
#include <assert.h>
template <class T>
class mat
{
private :
int n_;
int m_;
struct node{
T* x_;
node* next;
};
node *v_;
public:
int i,j;
void init (int N=1,int M=1)
{
assert(v_==null);
v_=new node[N];
if(v_==null)exit(0);
node* t=v_;
while(i<n-1){
t->next=++t;
i++;
}
t->next=null;
for (i=0;i<N;i++){
v_[i].x_=new T[M];
if(v_[i].x_==null)exit(0);}
n_=N;
m_=M;
}
void copy(T* v){
int k=0;
for ( i=0;i<n_*m_;i+=m_)
{
for(j=0;j<m_;j++)v_[k].x_[j]=v[i+j];
k++;
}
}
void copy(node* v){
i=1;
do{
for(j=0;j<m_;j++)v_[i].x_[j]=v->x_[j];
v=v->next;
i++;
}while(i<n_);
void set(T & val)
{
for( j=0;j<n_;j++)
for(int i=0;i<m_;i++)
v_[j].x_[i]=val;
}
void destroy(){
T* t;
for(int i=0;i<n_;i++){
t=v_[i].x_;
delete[]t;}
delete[]v_;
}
node* begin()const{ return v_;}
~mat(){
desroy();
}
mat(mat<T>& A ){
init(A.n_,A.m_);
copy(A.v_);}
mat(int N,int M,node * v){
init(N,M);
copy(v);
}
mat(int N,int M ,char* s){
init(N,M);
std::istrstream ins(s);
T* v1=new T[N*M];
for(i=0;i<N*M;i++)
ins>>v1;
init(N,M);
copy(v1);
}
mat<T>& newsize(int N,int M){
if((n_==N)&&(m_==M))return *this;
destroy();
init(N,M);
return *this;
}
mat<T>& operator=(mat<T> &A){
if(v_==A.v_)return *this;
if((n_==A.n_)&&(m_==A.m_))copy(A.v_);
else{
destroy();
init(A.n_,A.m_);
copy(A.v_);
return* this;
}
}
mat<T>& operator=(T& scalar){
set(scalar);
return *this;
}
int row()const{return n_;}
int col()const{return m_;}
friend istream& operator>>(istream& s,mat<T>&A){
int N,M;
s>>N>>M;
if((N!=A.n_)||(M!=A.m_)){
A.destroy();
init(N,M);
}
cout<<"請直接輸入矩陣各數"<<endl;
cout<<"第壹行:";
for(i=0;i<n_;i++){
for(j=0;j<m_;j++){
cin>>v_[i].x_[j];
cout<<'\t';}
cout<<'\n';
cout<<"第"<<(i+1)<<"行:";}
}
void print(){
for(i=0;i<n_;i++){
for(j=0;j<m_;j++)
cout<<"("<<i+1<<","<<j+1<<","<<v_[i].x_[j]<<")";
cout<< '\n';
}
}
friend ostream& operator<<(ostream& s,mat<T>& A){
print();
return s;
}
}
};
void main(){}