华剑缘
一切都在这个过程中获得,将那些目标埋藏于心中
posts - 19,comments - 20,trackbacks - 0
[求助] 关于拷贝构造函数,对象传递!!

#include 
< iostream.h >
class  Matrix
{
private :
    
int  rows,columns;
public :
        
int   ** pMatrix;
   Matrix( 
int  rows, int  columns);
   Matrix(Matrix
&  );
   
~ Matrix();
int  GetRows();
int  GetColumns();
void  SetValue();
void  Mul(Matrix a,Matrix b);
void  Mul(Matrix  * pa,Matrix  * pb);
void  Mul(Matrix  & a,Matrix  & b);

}
;

int  Matrix::GetRows() { return  rows;} ;
int  Matrix::GetColumns() { return  columns;} ;

// 构造函数
Matrix::Matrix( int  x, int  y)
{
     rows
= x;
         columns
= y;
        pMatrix
= new   int *  [x];
     
for ( int  i = 0 ; i < x; i ++ )
     pMatrix[i]
= new   int  [y];
}


// 析构函数
Matrix:: ~ Matrix()
{
        
for ( int  i = 0 ;i < rows;i ++ )
          delete[] pMatrix[i];
    delete[] pMatrix;
}


// 赋值函数
void  Matrix::SetValue()
{
    
int  i,j,value;
    
for ( i = 0 ; i < rows; i ++ )
      
{
           
for ( j = 0 ; j < columns; j ++ )
         
{
                cout
<< " " << i << " " ;
        cout
<< " " << j << " 列: " ;
        cin
>> value;
        cout
<< endl;
        pMatrix[i][j]
= value;
         }

       }

}


// 拷贝构造函数
Matrix::Matrix(Matrix &  M)
{  
     
for ( int  i = 0 ; i < M.rows; i ++ )
                 
for ( int  j = 0 ; j < M.columns; j ++ )
                   pMatrix[i][j]
= M.pMatrix[i][j];      /// //这里对不对?有什么更好的方式?
}


void  Matrix::Mul(Matrix a,Matrix b)
{
  Matrix c(a.GetRows(),b.GetColumns());
    
for ( int  i = 0 ;i < a.GetRows();i ++ ) {
                
for ( int  y = 0 ;y < b.GetColumns();y ++ ) {
          
if  (a.GetColumns() == b.GetRows())
            
for ( int  j = 0 ,x = 0 ;j < a.GetColumns(),x < b.GetRows ();j ++ ,x ++ )
                  c.pMatrix[i][y] 
+= a.pMatrix[i][j] * b.pMatrix[x][y];     /// //这里对不对?有什么更好的方式?

           
else   break ;
                        
                }

        }

   
}


// 主函数
void  main()
{
        Matrix Ma(
3 , 2 ),Mb( 2 , 2 );
        Ma.SetValue();
        Mb.SetValue();
    
for ( int  i;i < Ma.GetRows();i ++ )
                
for ( int  j;j < Ma.GetColumns();j ++ )
                        cout
<< Ma.pMatrix[i][j];   // 为什么编译运行后不能输出呢??

        Matrix Mc(
3 , 2 );    /// 觉得这样不妥,还有什么跟好的方法么
        Mc.Mul(Ma,Mb);     /// 这样也不对,怎么让两个Matrix对象相乘呢,有什么更好的方式么?
}


感谢大家热心指教。
一下是整理后的。
/////////////////////////////////////////////////
/////////////////////////////////////////////////

///////////////////Matrix.Class////////////////////////

#include <iostream.h>
//using namespace std;       //为什么不能在VC下正常使用

class Matrix
{
private:
    
int **pMatrix;
    
int rows,columns;
public:
static int ObjectAliveNo;
    Matrix(
int rows=0,int columns=0);
    Matrix(
const Matrix &M);
    
~Matrix();
Matrix
& operator=(const Matrix& M);
int GetRows() const;
int GetColumns() const;
int GetObjNo() const;
void SetValue();
void Mul(const Matrix a,const Matrix b);
void Mul(const Matrix *pa,const Matrix *pb);
void MUl(const Matrix &a,const Matrix &b);
friend Matrix 
operator~(Matrix& a);                                 //重载"~"操作符实现矩阵转置
friend Matrix operator*(const Matrix& a,const Matrix& b);          //重载"~"操作符实现矩阵相乘
//friend ostream& operator<<(const ostream& os,const Matrix& M);   //!!!
friend ostream& operator<<(ostream& os,const Matrix& M);
}
;

//构造函数
Matrix::Matrix(int x,int y)
{
    ObjectAliveNo
++;
    rows
=x;
    columns
=y;
      pMatrix
=new int *[rows];        //创建指针数组
      for(int i=0; i<rows; i++){
       pMatrix[i]
=new int [columns]; //真正实现二维数组
       for(int j=0; j<columns; j++)
         pMatrix[i][j]
=0;           //对二维数组初始化
     }

}


//拷贝构造函数函数
Matrix::Matrix(const Matrix& M)
{
    rows
=M.rows;
    columns
=M.columns;
    
//赋值前现分配空间!
    pMatrix=new int *[rows];
     
for(int m=0; m<rows; m++)
       pMatrix[m]
=new int [columns];
       
  
for(int i=0;i<rows;i++)
    
for(int j=0;j<columns;j++)
      pMatrix[i][j]
=M.pMatrix[i][j];
}


//析构函数
Matrix::~Matrix()
{
    ObjectAliveNo
--;
    
for(int i=0;i<rows;i++)
    delete[] pMatrix[i];        
//注意delete的顺序
    delete[] pMatrix;
}


//
int Matrix::GetRows() const {return rows;}            //
int Matrix::GetColumns() const {return columns;}      //
int Matrix::GetObjNo() const {return ObjectAliveNo;}  //对象数


//为矩阵赋值
void Matrix::SetValue()
{
  cout
<<"请对矩阵的每一项赋值:"<<endl;
    
int i,j,value;
    
for(i=0;i<rows;i++)
      
for(j=0;j<columns;j++){
        cout
<<"第 "<<i+1<<"";
        cout
<<""<<j+1<<"列:";
        cin
>>value;
        pMatrix[i][j]
=value;
      }

}


//重载"="操作符实现矩阵之间赋值
Matrix& Matrix::operator=(const Matrix& M)
{
     
if(this != &M){
    
for (int ii = 0 ;ii < rows;ii++ )
      
if(pMatrix[ii])
      delete[] pMatrix[ii];
   
if(pMatrix)
   delete[] pMatrix;
   rows 
= M.rows;
   columns 
= M.columns;
  
//分配存储空间
   pMatrix = new int* [rows];
   
for (int k=0 ;k<rows ;k++ )
   pMatrix[k] 
= new int[columns];

   
for ( int i=0; i<rows; i++ )
     
for ( int j = 0 ; j < columns; j ++ )
      pMatrix[i][j] 
= M.pMatrix[i][j];
 }

return *this;
}


//调用函数实现矩阵相乘操作
void Matrix::Mul(const Matrix a,const Matrix b)
{
    Matrix c(a.GetRows(),b.GetColumns());
    
if(a.GetColumns()==b.GetRows()){
       
int temp=0;
       
for(int i=0;i<a.GetRows();i++)
           
for(int j=0;j<b.GetColumns();j++){
              
for(int k=0;k<a.GetColumns();k++)
              temp
=temp+a.pMatrix[i][k]*b.pMatrix[k][j];
              c.pMatrix[i][j]
=temp;
              temp
=0;
           }

    }

//输出相乘结果
     for(int i=0;i<c.GetRows();i++){
    cout
<<'\n';
        
for(int y=0;y<c.GetColumns();y++)
              cout
<<c.pMatrix[i][y]<<' ';
        }

}


//重载操作符"*"实现矩阵相乘操作
Matrix operator*(const Matrix& a,const Matrix& b)
{
if (a.columns == b.rows){
  Matrix c(a.rows,b.columns);
   
for ( int i = 0 ;i < a.rows;i ++ ){
    
for ( int j = 0 ;j < b.columns;j ++ ){
       
for ( int columnIndex= 0 ;columnIndex < a.columns;columnIndex++ )
         c.pMatrix[i][j] 
+= a.pMatrix[i][columnIndex] * b.pMatrix[columnIndex][j];
       }

    }

    
return c;
  }

else
return Matrix();
}


//重载"~"操作符实现矩阵转置
Matrix operator~(Matrix& a)
{
    Matrix b(a.columns,a.rows);
    
for(int i=0;i<a.rows;i++)
     
for(int j=0;j<a.columns;j++)
      b.pMatrix[j][i]
=a.pMatrix[i][j];
      
      
return b;
}



//对"cout"进行重定义
ostream& operator<<(ostream& os,const Matrix& M){
  
for (int i = 0;i < M.rows;i++ ){
     
for (int j = 0;j < M.columns;j++ )
       os 
<< M.pMatrix[i][j] << " ";
       os 
<< endl;
  }

 
return (os << endl);
}


//静态成员赋初值!
int Matrix::ObjectAliveNo=0;

//主函数
int main()
{
    Matrix Ma(
6,3),Mb(3,5);
    Ma.SetValue();
    Mb.SetValue();
cout
<<'\n'<<"现在有"<<Ma.GetObjNo()<<"个矩阵"<<endl;     //前后对比检查各种函数对ObjectAliveNo的影响
cout<<Ma<<endl;
Matrix Mc;
Mc
=Ma*Mb;
cout
<<Mc;
cout
<<'\n'<<"现在有"<<Ma.GetObjNo()<<"个矩阵"<<endl;     //见上一条注释 !!出现错误:没有正常计数

cout
<<"Ma的转置:"<<endl;
Matrix Me;
Me
=~Ma;
cout
<<Me<<endl;

Matrix Md;
Md.Mul(Ma,Mb);
cout
<<'\n'<<"现在有"<<Ma.GetObjNo()<<"个矩阵"<<endl;    //有没有正常计数 


return 0;
}

posted on 2006-04-11 13:22 华剑缘 阅读(705) 评论(6)  编辑 收藏 引用

FeedBack:
# re: [求助] 关于拷贝构造函数,对象传递!!
2006-04-11 15:01 | 芋头
1、构造函数里没有对pMatrix初始化为0值,将导致后面有些地方错误。
2、拷贝构造函数和默认构造函数只会调用一个,你的拷贝构造函数中没有初始化pMatrix以及rows和columns。
3、Matrix::Mul中,if可以写在外面;由于构造函数中没有初始化0,c.pMatrix[i][y] +=这里肯定是错误的;另外,既然a.GetColumns() == b.GetRows(),就没有必要用j和x这2个变量了,一个就行了。
4、main函数里面的2个for循环,怎么i和j都不用初始化0的吗?
5、最后2行,是很不妥。可以考虑写成static,或写一个全局的operator*。

暂时只看出来这些。至于对错,这个最好自己调试。可以把算法转成自己看得懂的语言输出出来,比如:把矩阵a的第几行第几列和矩阵b的第几行第几列相乘,加到矩阵c的第几行第几列。这样的话,自己看得懂能排错就行了。  回复  更多评论
  
# re: [求助] 关于拷贝构造函数,对象传递!!
2006-04-11 16:05 | 任我行
void Matrix::Mul(Matrix a,Matrix b)给谁用呢?
  回复  更多评论
  
# re: [求助] 关于拷贝构造函数,对象传递!!
2006-04-12 13:44 | 史传红
看到了楼主的这篇文章,我试着改了一下,如下:希望大家一起加入讨论。

#include <iostream>
using namespace std;

class Matrix
{
private:
int rows,columns;
int **pMatrix;
public:
Matrix(int rows = 3,int columns = 2);
Matrix(const Matrix &M);
~Matrix();
Matrix& operator=(const Matrix& M);
int GetRows() const;
int GetColumns() const;
void SetValue();
friend Matrix operator*(const Matrix& a,const Matrix& b);
friend ostream& operator<<(ostream& os,const Matrix& M);
};

int Matrix::GetRows() const { return rows;}
int Matrix::GetColumns() const { return columns;}

// 构造函数
Matrix::Matrix(int x,int y)
{
rows = x;
columns = y;
//有的时候为了考虑创建对象的效率,在使用的时候分配存储空间,而不在构造函数中分配
pMatrix = new int* [x];
for (int i = 0 ; i < x; i++ )
{
pMatrix[i] = new int[y];
for(int j = 0;j < y;j++) //初始化每个值为0
pMatrix[i][j] = 0;
}
}
// 析构函数
Matrix::~Matrix()
{
for (int i = 0 ;i < rows;i ++ )
delete[] pMatrix[i];
delete[] pMatrix;
}

// 赋值函数
Matrix& Matrix::operator=(const Matrix& M)
{
if(this != &M)
{
for (int ii = 0 ;ii < rows;ii++ )
if(pMatrix[ii])
delete[] pMatrix[ii];
if(pMatrix)
delete[] pMatrix;
rows = M.rows;
columns = M.columns;
//分配存储空间
pMatrix = new int* [rows];
for (int k = 0 ; k < rows; k++ )
pMatrix[k] = new int[columns];

for ( int i = 0 ; i < rows; i ++ )
for ( int j = 0 ; j < columns; j ++ )
pMatrix[i][j] = M.pMatrix[i][j];
}
return *this;
}
void Matrix::SetValue()
{
int i,j,value;
for ( i = 0 ; i < rows; i ++ )
{
for ( j = 0 ; j < columns; j ++ )
{
cout << " 第 " << i << " 行 " ;
cout << " 第 " << j << " 列: " ;
cin >> value;
cout << endl;
pMatrix[i][j] = value;
}
}
}
// 拷贝构造函数
Matrix::Matrix(const Matrix& M)
{
rows = M.rows;
columns = M.columns;
//分配存储空间
pMatrix = new int* [rows];
for (int k = 0 ; k < rows; k++ )
pMatrix[k] = new int[columns];

for ( int i = 0 ; i < rows; i ++ )
for ( int j = 0 ; j < columns; j ++ )
pMatrix[i][j] = M.pMatrix[i][j];
}

Matrix operator*(const Matrix& a,const Matrix& b)
{
if (a.columns == b.rows)
{
Matrix c(a.rows,b.columns);
for ( int i = 0 ;i < a.rows;i ++ )
{
for ( int j = 0 ;j < b.columns;j ++ )
{
for ( int columnIndex= 0 ;columnIndex < a.columns;columnIndex++ )
c.pMatrix[i][j] += a.pMatrix[i][columnIndex] * b.pMatrix[columnIndex][j];
}
}
return c;
}
else
return Matrix();
}

ostream& operator<<(ostream& os,const Matrix& M)
{
for (int i = 0;i < M.rows;i++ )
{
for (int j = 0;j < M.columns;j++ )
os << M.pMatrix[i][j] << " ";
os << endl;
}
return (os << endl);
}


// 主函数
void main()
{
Matrix Ma(3,2),Mb(2,2);
Ma.SetValue();
Mb.SetValue();
cout << Ma << endl;
cout << Mb << endl;

Matrix Mc = Ma * Mb;//拷贝构造函数
cout << Mc << endl;
Mc = Mb; //=运算符,即赋值函数
cout << Mb << endl;
}   回复  更多评论
  
# re: [求助] 关于拷贝构造函数,对象传递!!
2006-04-12 20:53 | roa420
还是有错,编译出现7个错误!
ostream& operator<<(ostream& os,const Matrix& M) 中没有权限访问类中私有的rows和columns 这两个成员。
  回复  更多评论
  
# re: [求助] 关于拷贝构造函数,对象传递!!
2006-04-12 21:49 | 华剑缘
不是呀,我这里编译,运行都没出错呀  回复  更多评论
  
# re: [求助] 关于拷贝构造函数,对象传递!!
2006-04-13 12:48 | 史传红
@roa420
我在Visual C++ 6.0中编译的时候也出现过问题,我怀疑它对友元支持的不好。
建议你换一个编译器试试看。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理