青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

華劍緣
一切都在這個過程中獲得,將那些目標埋藏于心中
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 華劍緣 閱讀(750) 評論(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中編譯的時候也出現過問題,我懷疑它對友元支持的不好。
建議你換一個編譯器試試看。  回復  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲国语精品自产拍在线观看| 亚洲国产小视频| 亚洲国内欧美| 亚洲缚视频在线观看| 夜夜狂射影院欧美极品| 国产午夜精品全部视频在线播放| 美女诱惑一区| 亚洲国内精品| 欧美v亚洲v综合ⅴ国产v| 欧美在线www| 国产精品五月天| 亚洲综合精品四区| 久久精品综合一区| 日韩网站在线| 欧美1区免费| 蜜乳av另类精品一区二区| 国产日产亚洲精品| 欧美国产大片| 亚洲视频成人| 免费美女久久99| 日韩视频在线观看免费| 久久久久久自在自线| 亚洲国产视频一区| 亚洲精品乱码久久久久久黑人 | 亚洲欧美日韩精品久久亚洲区 | 欧美久久电影| 亚洲精品国产精品国自产在线 | 亚洲综合欧美日韩| 亚洲人午夜精品| 国产伦精品一区二区三区照片91 | 奶水喷射视频一区| 91久久精品日日躁夜夜躁国产| 久久精品国产亚洲一区二区| 日韩午夜电影| 久久国产欧美日韩精品| 亚洲免费激情| 在线精品一区二区| 国产日韩欧美在线视频观看| 欧美精品激情在线| 久久久蜜桃一区二区人| 国产精品99久久久久久www| 亚洲欧洲日本国产| 久久久久久久网站| 亚洲欧美国产精品桃花| 在线观看国产成人av片| 国产一级久久| 樱花yy私人影院亚洲| 国外成人在线视频| 国产欧美日韩一区| 国产精品久久久久aaaa| 欧美性事免费在线观看| 母乳一区在线观看| 猛男gaygay欧美视频| 亚洲精品国产精品国自产在线| 99国产麻豆精品| 亚洲精品一区在线观看| 国产日韩精品综合网站| 欧美日本精品在线| 老司机精品福利视频| 欧美成人福利视频| 欧美日韩另类在线| 国产精品乱码久久久久久| 国产精品视频| 亚洲欧洲一区二区三区久久| 99热在这里有精品免费| 亚洲婷婷国产精品电影人久久| 亚洲一区二区免费在线| 欧美a级片一区| 亚洲美女精品一区| 亚洲午夜激情网页| 亚洲精品国产欧美| 亚洲制服欧美中文字幕中文字幕| 久久久久久久97| 欧美成人自拍视频| 伊人成年综合电影网| 91久久国产综合久久| 午夜精品一区二区三区电影天堂 | 精品不卡在线| 亚洲国产精品国自产拍av秋霞| 99成人在线| 久久天天狠狠| 在线中文字幕日韩| 欧美电影电视剧在线观看| 欧美体内谢she精2性欧美| 一区二区视频免费在线观看| 亚洲图片在区色| 亚洲视频一区二区| 99av国产精品欲麻豆| 欧美xart系列高清| 久久久欧美精品| 亚洲黄一区二区| 久久久久一区二区| 伊人婷婷久久| 亚洲成人在线视频播放| 亚洲欧美区自拍先锋| 国产精品高潮呻吟| 午夜在线视频观看日韩17c| 中国成人黄色视屏| 国产私拍一区| 欧美成人一区二区三区| 欧美精品亚洲精品| 性色一区二区三区| 久久婷婷av| 一本久久a久久免费精品不卡| 中日韩在线视频| 精品成人国产| 亚洲视频一区二区| 在线观看视频一区| 在线一区欧美| 国产精品99久久久久久有的能看| 亚洲视频在线观看一区| 亚洲欧美日韩视频一区| 国产日韩视频| aa亚洲婷婷| 136国产福利精品导航网址| 亚洲精品中文字幕女同| 黄色成人在线网址| 在线一区二区日韩| 亚洲人成网站精品片在线观看| 亚洲欧美日韩国产成人精品影院 | 欧美福利一区二区| 国产精品欧美久久久久无广告| 免费日韩精品中文字幕视频在线| 国产精品二区在线| 亚洲一区中文| 亚洲人成网站777色婷婷| 久久久99精品免费观看不卡| 久久久久久精| 99综合精品| 狠狠色丁香婷婷综合| 欧美久久久久中文字幕| 亚洲视频欧洲视频| 久久久久久久97| aⅴ色国产欧美| 激情文学一区| 午夜精品久久久久久久久久久久| 国产一区999| 欧美日韩一区在线观看| 久久免费高清| 欧美一区二区视频在线观看2020| 亚洲国产99| 你懂的网址国产 欧美| 欧美一区二区三区四区高清| 日韩亚洲欧美成人| 在线日韩av| 欧美日韩国产成人精品| 亚洲视频图片小说| 裸体女人亚洲精品一区| 亚洲欧洲一区二区在线播放| 午夜视黄欧洲亚洲| 亚洲国产精品成人va在线观看| 欧美国内亚洲| 欧美在线日韩精品| 亚洲靠逼com| 欧美高清视频在线| 亚洲精品视频在线看| 国产精品一区毛片| 欧美77777| 久久久亚洲国产天美传媒修理工 | 亚洲第一区在线| 99日韩精品| 亚洲人成网站在线播| 国产欧美日韩一区| 国产精品第三页| 欧美日韩国内| 欧美刺激性大交免费视频| 久久国产精品毛片| 亚洲一区二区三区在线播放| 欧美丰满高潮xxxx喷水动漫| 久久综合色影院| 久久久爽爽爽美女图片| 欧美一区二区视频在线观看| 亚洲欧美日本精品| 亚洲欧美综合国产精品一区| 亚洲一卡久久| 久久久久青草大香线综合精品| 久久精品视频在线| 久久夜色精品国产欧美乱| 久久精品一区中文字幕| 国产伦精品一区二区三| 国产一区二区观看| 欧美日韩大片一区二区三区| 欧美伊久线香蕉线新在线| 欧美大学生性色视频| 久久在精品线影院精品国产| 亚洲一区在线播放| 国产一区二区观看| 国产精品捆绑调教| 久久综合综合久久综合| 在线视频中文亚洲| 先锋影院在线亚洲| 亚洲久久一区二区| 国产精品theporn| 欧美激情国产日韩| 久久久久国色av免费观看性色| 麻豆免费精品视频| 亚洲一区欧美激情| 亚洲欧美日韩第一区| 亚洲精品久久久久久久久久久久 | 欧美一区激情视频在线观看|