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

華劍緣
一切都在這個過程中獲得,將那些目標埋藏于心中
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>
            亚洲国产二区| 99天天综合性| 国产精品成人免费视频| 在线一区二区三区四区五区| 亚洲制服少妇| 久久国产精彩视频| 亚洲精品欧美极品| 国产欧美日韩综合一区在线观看| 久久午夜激情| 欧美黄色一级视频| 久久久国产精品一区二区中文| 亚洲日韩欧美视频一区| 国产日韩在线不卡| 欧美日韩一区二区三区在线观看免 | 亚洲国产精品福利| 亚洲欧美日韩国产成人| 亚洲国产99| 欧美成人日本| 欧美亚洲一区三区| 亚洲天堂成人在线视频| 亚洲破处大片| 免费不卡视频| 亚洲高清视频在线观看| 亚洲高清视频在线| 蜜臀a∨国产成人精品| 久久不见久久见免费视频1| 蜜桃视频一区| 欧美韩国日本综合| 久久综合一区| 亚洲精品视频免费在线观看| 亚洲电影av| 在线成人小视频| 亚洲人成欧美中文字幕| 亚洲国产婷婷综合在线精品| 欧美大片免费观看| 一区二区冒白浆视频| 一区二区三区四区在线| 亚洲精品女av网站| 久久视频在线看| 一区二区三区国产精品| 一本色道久久加勒比精品| 揄拍成人国产精品视频| 欧美经典一区二区| 伊人狠狠色j香婷婷综合| 在线观看欧美精品| 亚洲国产高清高潮精品美女| 亚洲欧美在线免费| 久久久最新网址| 麻豆成人在线观看| 99re6热只有精品免费观看 | 宅男噜噜噜66一区二区| 99这里有精品| 欧美一区二区三区视频免费| 欧美一站二站| 久久久综合网站| 男女激情视频一区| 久久久999成人| 欧美成人精品一区二区三区| 欧美日韩精品一本二本三本| 国产日韩欧美一二三区| 亚洲国产乱码最新视频| 亚洲女性裸体视频| 亚洲国产日韩在线一区模特| 美女日韩欧美| 亚洲精品国精品久久99热| 亚洲午夜在线观看| 国产精品国产三级国产aⅴ入口| 国产午夜精品一区二区三区视频| 狠狠色伊人亚洲综合成人| 国产精品久久毛片a| 一区二区欧美精品| 久久久精品国产99久久精品芒果| 亚洲电影免费观看高清| 久久综合网络一区二区| 国产欧美精品日韩区二区麻豆天美| 在线观看av一区| 国产日韩在线视频| 久久久久久久成人| 亚洲人成小说网站色在线| 亚洲欧美国产不卡| 国产精品尤物| 一本到12不卡视频在线dvd| 久久成人18免费观看| 午夜视频一区二区| 欧美午夜久久| 最新热久久免费视频| 亚洲精品一区二区三区四区高清| 亚洲国产欧美一区| 久久精品99无色码中文字幕| 欧美午夜片在线免费观看| 亚洲欧美日韩国产中文| 亚洲日本黄色| 欧美中文字幕第一页| 老司机免费视频一区二区三区| 亚洲国产欧美在线人成| 久久久久久久综合色一本| 亚洲午夜性刺激影院| 久久精品盗摄| 国产欧美精品一区| 亚洲欧美久久久| 一区二区国产日产| 欧美日韩免费一区| 一区二区三区欧美亚洲| 欧美在线国产精品| 国产日产欧美一区| 亚洲国产精品久久久久久女王| 亚洲精品少妇网址| 欧美77777| 韩国一区电影| av成人免费在线| 欧美日韩一区免费| 一本久道久久综合中文字幕| 亚洲欧美一区在线| 国产亚洲欧美日韩美女| 香蕉久久精品日日躁夜夜躁| 欧美粗暴jizz性欧美20| 国产精品伊人日日| 久久久精品国产免大香伊| 欧美一区二区三区视频在线观看| 亚洲美女啪啪| 亚洲视频在线一区观看| 国产精品裸体一区二区三区| 亚洲欧美日韩在线不卡| 亚洲成色www8888| 你懂的亚洲视频| 你懂的一区二区| 免费观看在线综合| 久久久国产一区二区三区| 午夜精品久久久久久久99樱桃| 欧美日韩在线大尺度| 亚洲国产另类 国产精品国产免费| 国产欧美午夜| 西瓜成人精品人成网站| 欧美在线观看天堂一区二区三区| 欧美日韩亚洲另类| 一区二区三区日韩在线观看| 亚洲少妇一区| 欧美老女人xx| 亚洲精品综合精品自拍| 99精品视频免费全部在线| 欧美精品手机在线| 亚洲欧洲一区| 一区二区三区四区国产| 欧美日韩一区二区免费视频| 日韩视频在线播放| 亚洲欧美另类中文字幕| 国产精品久久波多野结衣| 午夜精品福利一区二区三区av| 亚洲欧美日韩在线不卡| 国产偷自视频区视频一区二区| 香蕉久久国产| 你懂的成人av| 亚洲天堂偷拍| 国产视频精品免费播放| 久久国产婷婷国产香蕉| 免费日韩视频| 99精品国产99久久久久久福利| 国产精品福利久久久| 欧美亚洲日本网站| 亚洲福利视频网站| 午夜精品免费在线| 怡红院精品视频| 欧美日韩成人在线观看| 亚洲欧美电影在线观看| 国产精品一级在线| 欧美国产日韩a欧美在线观看| 亚洲乱码国产乱码精品精可以看| 欧美日本韩国在线| 欧美自拍丝袜亚洲| 亚洲精品日韩一| 久久精品亚洲一区| 亚洲精品永久免费精品| 国产精品视频最多的网站| 久久嫩草精品久久久精品| 亚洲最黄网站| 欧美华人在线视频| 欧美亚洲在线视频| 日韩视频在线一区二区| 国产三级精品三级| 欧美日韩不卡| 久久综合中文字幕| 亚洲专区一区二区三区| 亚洲国产精品成人va在线观看| 亚洲免费网址| 亚洲精品国产视频| 国产性猛交xxxx免费看久久| 欧美日韩p片| 久久久亚洲精品一区二区三区| 一区二区三区久久精品| 欧美国产乱视频| 久久不射电影网| 亚洲一区二区免费看| 最新热久久免费视频| 国内激情久久| 国产精品专区第二| 欧美日韩一区二区三区四区五区 | 亚洲一区制服诱惑| 亚洲免费成人| 91久久精品国产91久久性色| 国产主播精品|