Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

operator in C++

0 views
Skip to first unread message

赤貧白領階級學生

unread,
Sep 29, 1997, 3:00:00 AM9/29/97
to

小弟有個C++的程式片段想請各位網友幫忙看看,看是錯在哪裡,
希望各位網友不吝指教.

//Matrix.h
//小弟自己定的class
#if !defined (__MATRIX_H)
#define __MATRIX_H

class CMatrix {
double *m_dblArray;
int m_intRow, m_intCol;
public:
CMatrix(int intRow, int intCol, double *dblInit);
int Row(void);
int Col(void);
double Item(int intRow, int intCol);
CMatrix operator+(CMatrix udtAddend);
CMatrix operator-(CMatrix udtSubtrahend);
CMatrix operator*(CMatrix udtMultiplier);
void Output(char *strFilename, char *strFormat);
~CMatrix();
};

#endif //!defined (__MATRIX_H)

//Matrix.cpp
//我只將用的到的列出來
CMatrix::CMatrix(int intRow, int intCol, double *dblInit)
{
int i,j;

m_intRow = intRow;
m_intCol = intCol;
m_dblArray = new double[m_intRow * m_intCol];
for(i=0 ; i<m_intRow ; i++)
for(j=0 ; j<m_intCol ; j++)
*(m_dblArray+i*m_intCol+j) = *(dblInit+i*m_intCol+j);
}

double CMatrix::Item(int intRow, int intCol)
{
return *(m_dblArray+intRow*m_intCol+intCol);
}

CMatrix CMatrix::operator+(CMatrix udtAddend)
{
//assert(udtAddend.Row == m_intRow && udtAddend.Col == m_intCol);
double *dblResult = new double[m_intRow * m_intCol];
int i,j;
for(i=0 ; i<m_intRow ; i++)
for(j=0 ; j<m_intCol ; j++)
*(dblResult+i*m_intCol+j) = Item(i,j) + udtAddend.Item(i,j);
CMatrix udtResult(m_intRow, m_intCol, dblResult);
delete[] dblResult;
return udtResult;
}

//FuzzyView.cpp
void CFuzzyView::OnFun1()
{
// TODO: Add your command handler code here
double dblInit1[] = { 2,4,3,1 };
double dblInit2[] = { 1,0,0,1 };
double dblInit3[4];
CMatrix udtM1(2,2,dblInit1);
CMatrix udtM2(2,2,dblInit2);
CMatrix udtM3(2,2,dblInit3);

udtM1.Output("M1.TXT","%8.3f");
udtM2.Output("M2.TXT","%8.3f");
udtM3 = udtM1 + udtM2;
udtM3.Output("M3.TXT","%8.3f");
}

當程式執行到udtM3 = udtM1 + udtM2;時會發生錯誤...
請問各位網友為什麼呢??

--
[m [1;31m※ 來源:‧蛋捲廣場 bbs.tku.edu.tw‧[FROM: dial31.20365190] [m

四眼的王蟲

unread,
Oct 1, 1997, 3:00:00 AM10/1/97
to

ilp...@csie.nctu.edu.tw (想找回原來的自己) wrote:

>不好意思, 沒時間測你的程式, 花了幾秒鐘看了一下, 覺得


>
> CMatrix operator+(CMatrix udtAddend);
> CMatrix operator-(CMatrix udtSubtrahend);
> CMatrix operator*(CMatrix udtMultiplier);

> ~~~~ ~~~~
> 加上 '&' 加上 '&'
>
>好像加上 reference 的符號比較好吧 ('&')

第一個 & 不能加否則會出問題。
第二個 & 加上比較好不過也應該在 CMatrix 前面
加上 const 才好。

四眼的王蟲

--
生命是在黑暗中閃爍的光

呂布

unread,
Oct 1, 1997, 3:00:00 AM10/1/97
to

※ 引述《dy...@ms1.hinet.net (四眼的王蟲)》之銘言:
: > CMatrix operator+(CMatrix udtAddend);

: > CMatrix operator-(CMatrix udtSubtrahend);
: > CMatrix operator*(CMatrix udtMultiplier);
: > ~~~~ ~~~~
: > 加上 '&' 加上 '&'
: >好像加上 reference 的符號比較好吧 ('&')

省記憶體空間!
因為如果你所定義的類別過大..,
以傳值的方式會很浪費記憶空間,
所以用reference比較好耶!

: 第一個 & 不能加否則會出問題。

不瞭?會出什麼問題??

: 第二個 & 加上比較好不過也應該在 CMatrix 前面
: 加上 const 才好。

為什麼要加上const?

--
※ Origin: 元智大學 風之塔 BBS <bbs.yzu.edu.tw> ◆ From: [140.138.144.194]

想找回原來的自己

unread,
Oct 1, 1997, 3:00:00 AM10/1/97
to

不好意思, 沒時間測你的程式, 花了幾秒鐘看了一下, 覺得

呂布

unread,
Oct 2, 1997, 3:00:00 AM10/2/97
to

※ 引述《jwc...@csie.nctu.edu.tw (Seven Roses + 1)》之銘言:
: : 不瞭?會出什麼問題??
: 我想是會 reference 到 local varible 吧?
: : 為什麼要加上const?
: 應該是遵從原 operator 的原意吧?

ㄟ...可不可以講清楚一點耶?...Thanks!

Seven Roses + 1

unread,
Oct 2, 1997, 3:00:00 AM10/2/97
to

呂布 (lhy...@bbs.yzu.edu.tw) 提到:
: ※ 引述《dy...@ms1.hinet.net (四眼的王蟲)》之銘言:
: 省記憶體空間!

: 因為如果你所定義的類別過大..,
: 以傳值的方式會很浪費記憶空間,
: 所以用reference比較好耶!
: : 第一個 & 不能加否則會出問題。
: 不瞭?會出什麼問題??

我想是會 reference 到 local varible 吧?

: : 第二個 & 加上比較好不過也應該在 CMatrix 前面
: : 加上 const 才好。
: 為什麼要加上const?

應該是遵從原 operator 的原意吧?

0 new messages