我有我的任务,需要非常严格的帮助 - 你是否愿意回答这个问题呢?
1 - 将Matrix类转换为模板类。使用合适的主函数测试矩阵模板,该函数声明复数矩阵,键盘/文件输入和屏幕/文件输出。
因此原始代码如下
#ifndef MATRIX_H //include guard
#define MATRIX_H
#include <iostream> //generic IO
#include <fstream> //file IO
#include <stdexcept> //provides exceptions
#include "vector.h" //we use Vector in Matrix implementation
class Matrix {
private:
Vector v; // Vector used to store the matrix elements
int nrows; // number of rows of the matrix
int ncols; // number of columns of the matrix
public:
// CONSTRUCTORS
Matrix(); // default constructor, uses default constructor for v
Matrix(int Nrows, int Ncols); // alternate constructor
Matrix(const Vector& v); // build a matrix from a vector
Matrix(const Matrix& m); // copy constructor
// ACCESSOR METHODS
int getNrows() const; // get the number of rows
int getNcols() const; // get the number of cols
// OVERLOADED FUNCTION CALL OPERATORS
double& operator() (int i, int j); // function call overload (-,-) for assignment
double operator() (int i, int j) const; // for reading matrix values
Matrix& operator=(const Matrix& m); // overloaded assignment operator
bool operator==(const Matrix& m) const; // overloaded comparison operator
// INPUT & OUTPUT
friend std::istream& operator>>(std::istream& is, Matrix& m);// keyboard input
friend std::ostream& operator<<(std::ostream& os, const Matrix& m);// screen output
//the file output operator is compatible with file input operator,
//ie. everything written can be read later.
friend std::ifstream& operator>>(std::ifstream& ifs, Matrix& m);// file input
friend std::ofstream& operator<<(std::ofstream& ofs, const Matrix& m);// file output
};
// Note: There is no strict need for a copy constructor or
// assignment operator in the Matrix class since the
// compiler versions of these methods will work OK. It will copy/assign
// the values of nrows and ncols and call the Vector copy
// constructor/assignment operator automatically for the
// Vector part v inside the Matrix. However they are written
// for clarity and understanding
#endif
为了将其转换为模板,我使用模板
更改了功能#ifndef MATRIX_H //include guard
#define MATRIX_H
#include <iostream> //generic IO
#include <fstream> //file IO
#include <stdexcept> //provides exceptions
#include "vector.h" //we use Vector in Matrix implementation
class Matrix {
template <typename T> class T
protected:
Vector v; // Vector used to store the matrix elements
int num; // Number of elements
int nrows; // number of rows of the matrix
int ncols; // number of columns of the matrix
T* pdata; // Pointer to the data
void Init(int Num); // private function since user should not call it
public:
// CONSTRUCTORS
Matrix(); // default constructor, uses default constructor for v
Matrix(int Nrows, int Ncols); // alternate constructor
Matrix(const Matrix& v); // build a matrix from a vector
Matrix(const Matrix& m); // copy constructor
// SIZE
int size() const; //get number of elements in the vector
// ACCESSOR METHODS
int getNrows() const; // get the number of rows
int getNcols() const; // get the number of cols
// OVERLOADED FUNCTION CALL OPERATORS
Matrix<T>& operator() (int i, int j); // function call overload (-,-) for assignment
T& operator() (int i, int j) const; // for reading matrix values
T operator[] (int i) const; // overloaded array access operator for reading
Matrix& operator=(const Matrix& m); // overloaded assignment operator
bool operator==(const Matrix& m) const; // overloaded comparison operator
// INPUT & OUTPUT
friend std::istream& operator>>(std::istream& is, Matrix& m);// keyboard input
friend std::ostream& operator<<(std::ostream& os, const Matrix& m);// screen output
//the file output operator is compatible with file input operator,
//ie. everything written can be read later.
friend std::ifstream& operator>>(std::ifstream& ifs, Matrix& m);// file input
friend std::ofstream& operator<<(std::ofstream& ofs, const Matrix& m);// file output
};
// Note: There is no strict need for a copy constructor or
// assignment operator in the Matrix class since the
// compiler versions of these methods will work OK. It will copy/assign
// the values of nrows and ncols and call the Vector copy
// constructor/assignment operator automatically for the
// Vector part v inside the Matrix. However they are written
// for clarity and understanding
#endif
最后测试我在另一个类
输入的Matrix #include <iostream>
#include <cmath>
#include <vector.h>
#include <complex.h>
#include <matrix.h>
Matrix<Complex> matrix_vector(10);
std::cin >> matrix_vector;
请,我收到错误,并不完全确定我在所有地方是否正确。我非常感谢。非常感谢