【发布时间】:2018-07-05 07:21:34
【问题描述】:
我正在尝试使用我的 2D 数组程序并将类私有设置从 (UML CODE) -m_array: Array<T> 更改为 -m_array : T**。这是数据结构类的要求......其中一项作业
Array<TYPE> m_array; // m_array : Array<TYPE>
到
TYPE ** m_array; // m_array : T**
Array2D.h
template <class TYPE>
class Array2D
{
public:
Array2D();
Array2D(int row, int col);
Array2D(const Array2D<TYPE> & copy);
~Array2D() {}
const Array2D<TYPE> & operator=(const Array2D<TYPE>& rhs);
const Row<TYPE> operator[](int index) const;
Row<TYPE> operator[](int index);
const int getRow() const { return m_rows; }
void setRow(int rows);
const int getColumn() const { return m_cols; }
void setColumn(int columns);
TYPE & Select(int row, int col);
private:
Array<TYPE> m_array; // This needs to change
int m_rows;
int m_cols;
bool ValidCol(int column) { return (column < m_cols || column >= 0);}
bool ValidRow(int row) { return (row < m_rows || row >= 0); }
};
如果我将私有 Array<TYPE> m_array 更改为 TYPE ** m_array;,我会遇到一些 Array2D 函数的问题。不知道如何更改我的代码以使用双指针。
template<class TYPE>
Array2D<TYPE>::Array2D(int row, int col) : m_rows(row), m_cols(col)
{
cout << "User specified 2D constructor " << endl;
cout << "Row = " << row << " and Col = " << col << "... R*C = " << (row * col) << endl << endl;
if (m_rows < 1 || m_cols < 1)
throw Exception("Invalid dimensions");
else
m_array = Array<TYPE>(m_rows*m_cols); // <--- Issues
}
我收到类似的错误
...错误 C2440:'=':无法从 'Array' 转换为 'int **'
如果我不考虑上面的例子。
数组.h
template <class TYPE>
class Array
{
public:
Array();
Array(int length, int beg_index = 0);
Array(const Array<TYPE>& copy);
~Array();
const Array<TYPE> & operator=(const Array<TYPE>& rhs);
TYPE & operator[](int index);
const TYPE & operator[](int index) const;
int getLength() const { return m_length; }
inline void setLength(int new_length);
int getStartIndex() const {return m_start_index; }
void setStartIndex(const int new_start_index) {m_start_index = new_start_index;}
private:
bool WithinBounds(const int loc_request) const;
bool ValidLength(const int requested_length)const;
TYPE * m_array = 0;
int m_length;
int m_start_index;
};
您可以提出任何建议,我们将不胜感激。我从来没有完全理解指针,但有一个基本的掌握。
【问题讨论】:
-
为什么要
TYPE **?这太糟糕了。Array怎么了? -
T**不是矩阵的正确数据类型,矩阵不是列表列表。 -
数据结构类的要求是采用二维数组并使用双指针...如果需要我可以发布整个代码。
标签: c++ arrays double-pointer