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

華劍緣
一切都在這個過程中獲得,將那些目標埋藏于心中
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 華劍緣 閱讀(763) 評論(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   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            麻豆精品国产91久久久久久| 红桃视频欧美| 免费人成网站在线观看欧美高清| 欧美日韩视频| 欧美成黄导航| 国产亚洲a∨片在线观看| 一本到高清视频免费精品| 亚洲黄色一区二区三区| 欧美亚洲在线| 欧美一区二区三区四区在线观看地址| 欧美激情一区二区三区蜜桃视频 | 亚洲视频精品| 99热这里只有精品8| 欧美 日韩 国产一区二区在线视频| 久久精品一区二区三区四区| 国产精品美女一区二区在线观看 | 亚洲电影免费在线观看| 国产一区二区三区黄视频| 午夜精品短视频| 欧美在线观看一区二区| 国产精品午夜电影| 亚洲专区一区二区三区| 小黄鸭精品aⅴ导航网站入口| 欧美特黄一区| 亚洲一区二区高清| 久久爱www久久做| 国产女优一区| 久久精品国产亚洲a| 久久三级福利| 亚洲第一精品夜夜躁人人爽| 久久久久久久久久久久久9999| 美女精品自拍一二三四| 亚洲高清免费在线| 欧美韩国日本综合| 在线一区二区三区四区五区| 亚洲一区二区久久| 国产欧美精品一区二区三区介绍| 西西裸体人体做爰大胆久久久| 久久本道综合色狠狠五月| 国产农村妇女精品一二区| 久久精品人人| 亚洲日本在线视频观看| 亚洲自拍三区| 激情懂色av一区av二区av| 欧美aa国产视频| 亚洲视频1区2区| 麻豆av一区二区三区| 日韩视频不卡| 国产午夜精品理论片a级大结局| 久久久精品一品道一区| 亚洲国产精品va在线观看黑人| 亚洲一区免费视频| 狠狠色香婷婷久久亚洲精品| 女生裸体视频一区二区三区| 一区二区欧美精品| 免费观看欧美在线视频的网站| 一本色道久久精品| 国产视频一区在线| 欧美精品91| 久久精品国产999大香线蕉| 亚洲福利视频一区二区| 欧美一区三区三区高中清蜜桃| 在线精品亚洲一区二区| 欧美体内谢she精2性欧美| 欧美在线观看视频在线| 亚洲毛片一区| 欧美r片在线| 亚洲免费在线观看| 最新国产成人在线观看| 国产亚洲欧美一区| 欧美日一区二区三区在线观看国产免 | 久久精品视频导航| 国产精品99久久久久久有的能看| 欧美mv日韩mv亚洲| 欧美在线高清| 亚洲性色视频| 亚洲乱码精品一二三四区日韩在线 | 欧美美女视频| 久久久久久久999精品视频| 一区二区欧美日韩| 亚洲人被黑人高潮完整版| 久久久久欧美精品| 亚洲欧美文学| 亚洲视频电影图片偷拍一区| 91久久久精品| 影音先锋日韩有码| 国内外成人免费视频| 国产精品扒开腿做爽爽爽软件| 欧美大胆a视频| 另类酷文…触手系列精品集v1小说| 欧美一区二区黄色| 亚洲视频在线免费观看| 99国产精品国产精品久久| 亚洲国产二区| 欧美国产精品劲爆| 欧美成人国产一区二区| 鲁大师成人一区二区三区| 久久精品国产2020观看福利| 亚洲欧美综合| 欧美一区二区免费视频| 亚洲欧美日韩国产成人| 亚洲一品av免费观看| 一区二区三区www| 欧美在线影院| 欧美一级大片在线免费观看| 久久精品二区亚洲w码| 国产精品午夜电影| 国产精品女同互慰在线看| 欧美日韩一区二区三区| 在线视频精品一区| 亚洲欧美日韩中文在线制服| 免费成人激情视频| 国产麻豆视频精品| 欧美一区二区精美| 欧美激情精品| 国产麻豆视频精品| 亚洲美女精品成人在线视频| 欧美在线视频播放| 亚洲国产成人精品女人久久久| 在线性视频日韩欧美| 久久久久久亚洲精品不卡4k岛国| 美女国产一区| 欧美午夜无遮挡| 国产视频精品免费播放| 国产亚洲综合性久久久影院| 亚洲高清视频的网址| 一区二区三区欧美亚洲| 欧美一区二区三区四区在线观看地址| 裸体歌舞表演一区二区| 亚洲精品久久久久久久久| 香蕉久久国产| 欧美国产亚洲视频| 国产一区久久| 一本色道久久综合亚洲二区三区| 久久久久久久波多野高潮日日 | 国内不卡一区二区三区| 亚洲视频免费看| 亚洲国产精品电影| 久久久国产91| 国产日韩欧美视频在线| 亚洲欧美日韩直播| 99国内精品久久| 欧美高清视频在线观看| 亚洲国产精品va在看黑人| 久久噜噜亚洲综合| 欧美一区二区高清| 国产精品日韩久久久| 亚洲一区二区免费| 99ri日韩精品视频| 蜜臀va亚洲va欧美va天堂| 国产伦精品一区二区三区视频黑人 | 欧美黄网免费在线观看| 国产精品揄拍500视频| 亚洲第一区在线| 欧美在线一二三四区| 日韩视频精品在线观看| 久久亚洲精品中文字幕冲田杏梨| 国产精品资源在线观看| 亚洲区欧美区| 免费在线观看日韩欧美| 午夜在线一区二区| 国产人成一区二区三区影院| 亚洲一区二区三区四区视频| 欧美激情偷拍| 欧美一级一区| 国产一区深夜福利| 午夜精品免费在线| 日韩小视频在线观看| 欧美肉体xxxx裸体137大胆| 亚洲一区二区三区中文字幕| 中国成人亚色综合网站| 国产伦精品一区二区三区免费迷 | 国产午夜精品美女视频明星a级| 久久gogo国模裸体人体| 欧美一站二站| 在线电影一区| 欧美a级片网| 久久午夜国产精品| 亚洲成人在线视频播放| 久久九九国产| 久久久久一区二区| 99综合电影在线视频| 亚洲四色影视在线观看| 国产一区日韩一区| 亚洲国产三级| 国产精品久久久久久久app| 久久精品视频网| 欧美不卡视频一区发布| 亚洲一级在线| 久久精品一区二区国产| 一本大道久久a久久综合婷婷| 亚洲视频欧美在线| 激情丁香综合| 亚洲毛片一区| 国产精品久久久久久久久久直播| 久久国产视频网| 久久精品论坛| 亚洲欧美国产精品va在线观看 | 夜夜夜久久久| 亚洲香蕉成视频在线观看|