程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c++-C++模板類的問題,求解答

c++-C++模板類的問題,求解答

編輯:編程綜合問答
C++模板類的問題,求解答
namespace mathematic {
template < typename T  >
class mathBasic {
 private:
  int column_n;
  int row_n;
 public:
  T **element;
  mathBasic(int , int  = 0);
  virtual ~mathBasic();
  virtual void show();
  int column() const {return column_n;};
  int row() const {return row_n;};
  int &column() {return column_n;};
  int &row() {return row_n;};
  T get(int , int = 0) const;
  void set(T val, int x, int y = 0);
  void set_whole(const mathBasic &);
  T max();
  T min();
  mathBasic& operator+(const mathBasic &);
  mathBasic& operator-(const mathBasic &);
  template<class S> friend std::ostream &operator<<(std::ostream& os, mathBasic<S> &M);
};
template< typename T > class Matrix: public mathBasic< T > {
 public:
  Matrix();
  Matrix(const Matrix<T> &);
  Matrix(int , int );
  Matrix(T **);
  Matrix<T>& operator*(const Matrix<T> &);
  void size() const;
  void matrix_row(int r) const;
  void matrix_column(int c) const;
  Matrix<T>& operator=(const Matrix<T> &);
  Matrix<T>& operator=(const mathBasic<T> &);
};
}
namespace mathemathic {
template <typename T>
mathBasic<T>::mathBasic(int r, int c) {
  if (!(r||c)) {
    std::cout << "error, no size!" << std::endl;
    element = null;
    row_n = column_n = 1;
  } else {
    if (0 == c) {
      column_n = 0;
      c = 1;
    } else {
      column_n = c;
    }
    row_n = r;
    element = new T*[r];
    for (int i = 0; i < r; i++)
      element[i] = new T[c]();
  }
}
template <typename T>
mathBasic<T>::~mathBasic() {
  for (int i = 0; i < row_n; i++)
    delete[] element[i];
  delete[] element;
  element = null;
}
template <typename T>
void mathBasic<T>::show() {
  int r, c;
  int *width = new int[column() == 0? row():column()]();
  int tempwid;
  std::string num;
  std::stringstream convert;
  if (0 == column_n) {
    c = 1;
  } else {
    c = column_n;
  }
  r = row_n;
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < r; j++) {
      convert << element[j][i];
      convert >> num;
      tempwid = num.size();
      width[j] = width[j] < tempwid? tempwid:width[j];
    }
  }
  for (int i = 0; i < r; i++)  {
    std::cout << "| ";
    for (int j = 0; j < c; j++) {
      std::cout.setf(std::ios_base::left);
      std::cout << std::setw(width[j]) << element[i][j] << " ";
    }
    std::cout << "|" << std::endl;
  }
  delete[] width;
}
template <typename T>
T mathBasic<T>::get(int r, int c) const{
  return element[r][c];
}
template <typename T>
void mathBasic<T>::set(T val, int x, int y) {
  element[x][y] = val;
}
template <typename T>
T mathBasic<T>::max() {
  T max = element[0][0];
  int r, c;
  if (0 == column_n) {
    c = 1;
  } else {
    c = column_n;
  }
  r = row_n;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
      max = (element[i][j] > max ? element[i][j]:max);
  }
  return max;
}
template <typename T>
T mathBasic<T>::min() {
  T min = element[0][0];
  int r, c;
  if (0 == column_n) {
    c = 1;
  } else {
    c = column_n;
  }
  r = row_n;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
      min = (element[i][j] < min ? element[i][j]:min);
  }
  return min;
}
template <typename T>
void mathBasic<T>::set_whole(const mathBasic<T> &one) {
  int r, c;
  r = one.row();
  if (one.column() == 0)
    c = 1;
  for (int i = 0; i < row(); i++)
    delete[] element[i];
  delete element;
  row() = one.row();
  column() = one.column();
  element = new T* [r];
  for (int i = 0; i < r; i++)
    element[i] = new T[c];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      element[i][j] = one.element[i][j];
    }
  }
}
template <typename T>
mathBasic<T>& mathBasic<T>::operator-(const  mathBasic<T> &m) {
  if (m.row_n != row_n||m.column_n != column_n) {
    std::cout << "Size not matching, can\'t add!" << std::endl;
    std::cout << "the left is returned" << std::endl;
    return *this;
  }
  int r, c;
  r = row_n;
  if ((c = column_n) == 0) {
    c = 1;
  }
  static mathBasic<T> temp(row_n, column_n);
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      temp.element[i][j] = element[i][j] - m.element[i][j];
    }
  }
  return temp;
}
template <typename T>
mathBasic<T>& mathBasic<T>::operator+(const mathBasic<T> &m) {
  if (m.row_n != row_n||m.column_n != column_n) {
    std::cout << "Size not matching, can\'t add!" << std::endl;
    std::cout << "the left is returned" << std::endl;
    return *this;
  }
  int r, c;
  r = row_n;
  if ((c = column_n) == 0) {
    c = 1;
  }
  static mathBasic<T> temp(row_n, column_n);
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      temp.element[i][j] = element[i][j] + m.element[i][j];
    }
  }
  return temp;
}
template <typename T> std::ostream& operator<< (std::ostream& os, mathBasic<T>& M) {
  M.show();
  return os;
}
template <typename T>
Matrix<T>::Matrix(const Matrix<T> & mat):mathBasic(mat.mathBasic<T>::row(), mat.mathBasic<T>::column()) {
  for (int i = 0; i < mathBasic<T>::row(); i++)
    for (int j = 0; j < mathBasic<T>::column(); j++)
      element[i][j] = mat.element[i][j];
}
template <typename T>
Matrix<T>::Matrix(T **m):mathBasic(2, 2) {
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
      element[i][j] = *((T*)m + 2*i + j);
}
template <typename T>
Matrix<T>::Matrix():mathBasic(2, 2) {
  for (int i = 0; i < 2; i++)
    element[i][i] = 1;
}
template <typename T>
Matrix<T>::Matrix(int m, int n):mathBasic(m, n) {}
void Matrix<T>::size() const {
  std::cout << mathBasic<T>::row() << " " << mathBasic<T>::column() << std::endl;
}
template <typename T>
void Matrix<T>::matrix_row(int r) const {
  if (r >= mathBasic<T>::row()) {
    std::cout << "error , exceed" << std::endl;
    return ;
  }
  std::cout << "| ";
  for (int i = 0; i < mathBasic<T>::column(); i++) {
    std::cout << element[r][i] << " ";
  }
  std::cout << "|" << std::endl;
}
template <typename T>
void Matrix<T>::matrix_column(int c) const {
  if (c >= mathBasic<T>::column()) {
    std::cout << "error , exceed" << std::endl;
    return ;
  }
  std::stringstream convert;
  std::string num;
  unsigned wide = 0;
  for (int i = 0; i < mathBasic<T>::row(); i++) {
    convert << element[i][c];
    convert >> num;
    wide = wide > num.size() ? wide:num.size();
  }
  for (int i = 0; i < mathBasic<T>::row(); i++)
    std::cout << "|" << std::setw(wide) << element[i][c] << "|" << std::endl;
}
template <typename T>
Matrix<T>& Matrix::operator*(const Matrix<T> &m) {
  if (mathBasic<T>::column() != m.mathBasic<T>::row()) {
    std::cout << "Can not mulity" << std::endl;
    return *this;
  }
  T sum;
  static Matrix<T> temp(mathBasic<T>::row(), m.mathBasic<T>::column());
  for (int i = 0; i < mathBasic<T>::row(); i++) {
    for (int j = 0; j <m.mathBasic<T>::column(); j++) {
      sum = 0;
      for (int k = 0; k < mathBasic<T>::column(); k++) {
        sum += element[i][k]*m.element[k][j];
      }
      temp.element[i][j] = sum;
    }
  }
  return temp;
}
template <typename T>
Matrix<T>& Matrix::operator=(const Matrix<T> &mp) {
  int r, c;
  if ((mathBasic<T>::row() == 0&&mp.mathBasic<T>::row() != 0) || (mathBasic<T>::row() != 0&&mp.mathBasic<T>::row() == 0)) {
    std::cout << "Different kind!" << std::endl;
    return *this;
  }
  if (element != null) {
    for (int i = 0; i < mathBasic<T>::row(); i++) {
      delete[] element[i];
    }
    delete[] element;
  }
  c = mp.mathBasic<T>::column();
  r = mp.mathBasic<T>::row();
  mathBasic<T>::row() = mp.mathBasic<T>::row();
  mathBasic<T>::column() = mp.mathBasic<T>::column();
  element = new T*[r];
  for (int i = 0; i < r; i++)
    element[i] = new T[c]();
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      element[i][j] = mp.element[i][j];
  return *this;
}
template <typename T>
Matrix<T>& Matrix::operator=(const mathBasic & mp) {
  int r, c;
  if ((mathBasic<T>::row() == 0&&mp.mathBasic<T>::row() != 0)||(mathBasic<T>::row() != 0&&mp.mathBasic<T>::row() == 0)) {
    std::cout << "Different kind!" << std::endl;
    return *this;
  }
  if (element != null) {
    for (int i = 0; i < mathBasic<T>::row(); i++) {
      delete[] element[i];
    }
    delete[] element;
  }
  c = mp.mathBasic<T>::column();
  r = mp.mathBasic<T>::row();
  element = new T* [r];
  for (int i = 0; i < r; i++)
    element[i] = new T[c]();
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      element[i][j] = mp.element[i][j];
  return *this;
}

}
編譯器報錯
Matrix.h: In copy constructor ‘mathematic::Matrix< T 


::Matrix(const mathematic::Matrix&)’:
Matrix.h:6:42: error: class ‘mathematic::Matrix< T >’ does not have any field named ‘mathBasic’
Matrix< T >::Matrix(const Matrix< T > & mat):mathBasic(mat.mathBasic
^
不知道為什麼錯了,求解答

最佳回答:


Matrix類裡報錯你貼mathBasic的代碼上來?!這咋給你看?

Cashey1991
OzhangsenO
OzhangsenO
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved