• <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>
            華劍緣
            一切都在這個過程中獲得,將那些目標埋藏于心中
            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 華劍緣 閱讀(725) 評論(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中編譯的時候也出現過問題,我懷疑它對友元支持的不好。
            建議你換一個編譯器試試看。  回復  更多評論
              
            武侠古典久久婷婷狼人伊人| 麻豆精品久久精品色综合| 久久国产精品久久精品国产| 伊人久久大香线蕉亚洲五月天| 国产呻吟久久久久久久92| 中文字幕亚洲综合久久2| 久久福利青草精品资源站免费 | 少妇精品久久久一区二区三区| 午夜福利91久久福利| 一本大道久久香蕉成人网| 久久免费视频1| 99999久久久久久亚洲| 久久久久亚洲精品无码蜜桃| 无码人妻久久一区二区三区免费丨| 伊人久久大香线蕉av不卡| 欧美一区二区三区久久综| 久久精品毛片免费观看| 99久久精品费精品国产| 亚洲美日韩Av中文字幕无码久久久妻妇 | 中文字幕久久波多野结衣av| 亚洲中文字幕无码久久综合网| 人妻无码αv中文字幕久久| 99热成人精品热久久669| 99久久精品免费| 一本久久精品一区二区| 久久免费的精品国产V∧| 99久久婷婷国产一区二区| 久久青青草原精品国产软件| 久久久久久久女国产乱让韩| 2021久久精品国产99国产精品| 国产99久久久国产精免费| 久久中文字幕人妻熟av女| 伊人热人久久中文字幕| 久久免费视频1| 久久WWW免费人成—看片| 中文字幕乱码人妻无码久久| 精品国产乱码久久久久久浪潮| 人妻少妇久久中文字幕| 性欧美大战久久久久久久| 伊人久久免费视频| 久久久久亚洲AV成人片|