線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1049
推到 Plurk!
推到 Facebook!

運算子重載(overloading operator) 的問題

尚未結案
senboy
一般會員


發表:18
回覆:7
積分:5
註冊:2005-01-07

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-15 22:34:27 IP:163.28.xxx.xxx 未訂閱
我用vector實做矩陣的四則運算,在做矩陣乘常數可交換( M*c 或 c*M )的 運算子重載時在加上c*M 後程式碼(一)compiler會出現下面的錯誤 Error: Unresolved external 'operator *(const int&, const matrix&)' referenced from D:\MATRIX11\MATRIX.OBJ 但如果用程式碼(二) 皆可正常運算 可是程式碼(二)只有 *= 是class的member function 二元的 * 不是class的 friend function 確可以正常通過compiler 請問 1.程式碼(一)問題是出在哪裏,實在找不出來 2.程式碼(二)為何不用設 * 的運算為friend乃可正常compiler,設為friend function 反而會出現跟程式碼(一)一樣的問題. p.s. 試過學程式碼(二) 在程式碼(一) c*M 的重載不設friend 並將code改 matrix temp=m; for(int i=0;i程式碼(一)
#pragma option -w-inl -w-pch
#include 
#include     using namespace std;    #define _NO_THROW             //  throw ()
#define _THROW_MATRIX_ERROR   //  throw (matrix_error)    class matrix_error
{
   public:
         matrix_error (const string& what_arg) {cout<
class matrix
{
        private:
    vector > _m;
          size_t Row,Col;
        public:
   // Constructors
         matrix (const matrix& m);
   matrix (size_t row = 6, size_t col = 6 );       // Assignment operators
   matrix& operator = (const matrix& m) _NO_THROW;
   
   // Value extraction method
   size_t RowNo() const { return Row; }
   size_t ColNo() const { return Col; }
   
   // Subscript operator
   T& operator () (size_t row, size_t col) _THROW_MATRIX_ERROR;
   T  operator () (size_t row, size_t col) const _THROW_MATRIX_ERROR;       // Unary operators
   matrix operator   () _NO_THROW { return *this; }
   matrix operator - () _NO_THROW;
   
   // Combined assignment - calculation operators
     matrix  operator * (const matrix& m) _THROW_MATRIX_ERROR; //dot multiple 
     matrix  operator * (const T& c) _NO_THROW;
     friend matrix operator * (const T& c,const matrix& m) _NO_THROW;
 };
 
//=========Methd define=======================================    //constructor
template 
inline matrix::matrix (size_t row, size_t col)
{ Row=row;
  Col=col;
   _m=*new vector > (row,vector(col,0));
 
}     // copy constructor
template 
inline matrix::matrix (const matrix& m)
{
    Row=m.Row;
    Col=m.Col;
    _m=m._m;
    
}    // assignment operator
template 
inline matrix& matrix::operator = (const matrix& m) _NO_THROW
{
    Row=m.Row;
    Col=m.Col;
    _m = m._m;
    return *this;
}    // subscript operator to get/set individual elements
template 
inline T& matrix::operator () (size_t row, size_t col) _THROW_MATRIX_ERROR
{
   if (row >= Row || col >= Col)
      REPORT_ERROR( "matrix::operator(): Index out of range!");
   return _m[row][col];
}    // subscript operator to get/set individual elements
template 
inline T matrix::operator () (size_t row, size_t col) const _THROW_MATRIX_ERROR
{  
   if (row >= Row || col >= Col)
      REPORT_ERROR( "matrix::operator(): Index out of range!");
   return _m[row][col];
}    // unary negation operator
template 
inline matrix matrix::operator - () _NO_THROW
{
   matrix temp(Row,Col);       for (size_t i=0; i 
inline ostream& operator << (ostream& ostrm, const matrix& m)
{
   for (size_t i=0; i < m.RowNo(); i  )
   {
      for (size_t j=0; j < m.ColNo(); j  )
      {
         T x = m(i,j);
         ostrm << x << '\t';
      }
      ostrm << endl;
   }
   return ostrm;
}    // ====================binary matrix operator===================
// binary matrix dot multiplication
template  
inline matrix matrix::operator * (const matrix& m) _THROW_MATRIX_ERROR
{ matrix temp(*this);
          if ( Row != m.Row || Col != m.Col)
       REPORT_ERROR( "matrix::operator = : Inconsistent matrix sizes in addition!");        for (size_t i=0; i < m.Row; i  )
      for (size_t j=0; j < m.Col; j  )
             temp._m[i][j] =_m[i][j]*m._m[i][j];
    return temp;
}    // binary right scalar multiplication
template  
inline matrix matrix::operator * (const T& c) _NO_THROW
{  matrix temp(*this);
    for (size_t i=0; i < Row; i  )
           for (size_t j=0; j < Col; j  )
            temp._m[i][j]*= c;
    return temp;
  }    // binary left scalar multiplication
template 
matrix& operator * (const T& c, const matrix& m) _NO_THROW
   {  return(m*c);
        }    //=======================================================        
void main()
{
  matrix Image(3,3);      Image(0,0)=4;
  Image(1,0)=2;
  Image(1,1)=8;
  Image(2,2)=5;      matrix Image3(3,3);      cout<    程式碼(二) 只po差異的部分    
  //=========class matrix declear==============================
template 
class matrix
{
    // Combined assignment - calculation operators
    matrix& operator *= (const T& c) _NO_THROW;
 };    // ============combined operator and assignment=====================    // combined scalar multiplication and assignment operator
template  
inline matrix& matrix::operator *= (const T& c) _NO_THROW
{
    for (size_t i=0; i < Row; i  )
           for (size_t j=0; j < Col; j  )
            _m[i][j] *= c;
    return *this;
}    // ======base binary operator using comine operator to implement===
// binary right scalar multiplication operator
template  
inline matrix operator * (const matrix& m, const T& no) _NO_THROW
{
   matrix temp = m;
   temp *= no;
   return temp;
}    // binary left scalar multiplication operator
template  
inline matrix operator * (const T& no, const matrix& m) _NO_THROW
{
   return (m * no);
}
發表人 - senboy 於 2005/03/15 22:45:02
senboy
一般會員


發表:18
回覆:7
積分:5
註冊:2005-01-07

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-16 18:49:56 IP:163.28.xxx.xxx 未訂閱
自問自答一下 在版大pwipwi的文章http://delphi.ktop.com.tw/topic.php?topic_id=48746 找到解答了,似乎是bcb6 template對friend 實作的問題 程式碼(一)要改成
     
  template 
  class matrix;                        template 
  matrix operator * (const T& c,const matrix& m) _NO_THROW;
  //class定義前要先宣告 class 和 friend function      template 
  class matrix{
        ....
        friend matrix operator * < >(const T& c,const matrix& m) _NO_THROW;
        //                          ^^^^要多一個這個
  
這樣就不會有問題了 如果要跟程式碼二一樣不設friend 前面先宣告的部分可以不用 但是實作要改成如下
// binary right scalar multiplication
template  
inline matrix matrix::operator * (const T& c) _NO_THROW
{  matrix temp(*this);
    for (size_t i=0; i < Row; i  )
           for (size_t j=0; j < Col; j  )
            temp._m[i][j]*= c;
    return temp;
  }
// binary left scalar multiplication
template 
matrix operator * (const T& c, const matrix& m) _NO_THROW
{   matrix temp=m;
    for (size_t i=0; i < temp.RowNo(); i  )
           for (size_t j=0; j < temp.ColNo(); j  )
            temp(i,j)*=c;
    return temp;
   //不能用 return(m*c) compiler 會把這裏的 * 當做這個正在做的* 
        所以會認為不合參數的型式 即沒有實做 matrix*int
        不會使用上面單一參數的* ,應該是因為這個functon不是matrix 的member function
        所以不會使用matrix 裏定的operator,不知道對不對..
    
        }    
系統時間:2024-05-07 8:50:03
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!