【发布时间】:2020-11-27 15:05:45
【问题描述】:
尝试为我的 2D 数组类 Matrix3D 编写访问器,但是编译器给了我关于 c 样式代码的问题,我希望在这种情况下可以正常工作。有问题的错误是
错误 C2440:'return':无法从 'const T [3]' 转换为 'const T'
使用我的访问器时:
const T operator()(eMatrix3D::eMatrix3DElements) const
T operator()(eMatrix3D::eMatrix3DElements)
希望获得有关问题根本原因的一些反馈,以及有关如何使我的代码与 std::array<std::array<T, 3>, 3> m_elem 而不是 T m_elem[3][3] 兼容的反馈,以及需要注意哪些差异?
请在下面找到示例代码:
namespace eMatrix3D {
//////////////////////////////////////////////////////////////////////////
// *** ENUMS ***
//////////////////////////////////////////////////////////////////////////
enum eMatrix3DElements
{
e11 = 0,
e12,
e13,
e21,
e22,
e23,
e31,
e32,
e33
};
}
template <typename T>
class Matrix3D
{
public:
//////////////////////////////////////////////////////////////////////////
// *** CONSTRUCTORS ***
//////////////////////////////////////////////////////////////////////////
/// Default Constructor
//
Matrix3D(void);
/// External initialization Constructor
//
Matrix3D(const T& p_11, const T& p_12, const T& p_13,
const T& p_21, const T& p_22, const T& p_23,
const T& p_31, const T& p_32, const T& p_33);
//////////////////////////////////////////////////////////////////////////
// *** DESTRUCTOR ***
//////////////////////////////////////////////////////////////////////////
/// Destructor
//
~Matrix3D(void);
...
//////////////////////////////////////////////////////////////////////////
// *** PUBLIC METHODS (Accessors) ***
//////////////////////////////////////////////////////////////////////////
/// Get matrix m_element
/// \note 1-indexed
//
const T operator()(eMatrix3D::eMatrix3DElements) const;
/// Get matrix m_elem (non-const version)
/// \note 1-indexed
//
T operator()(eMatrix3D::eMatrix3DElements);
...
private:
/// Matrix m_elements
//
T m_elem[3][3]; //std::array<std::array<T, 3>, 3> m_elem;
}
//////////////////////////////////////////////////////////////////////////
// *** PUBLIC METHODS (Accessors) ***
//////////////////////////////////////////////////////////////////////////
/// Get matrix m_elem
/// \note 1-indexed
//
template <typename T>
const T Matrix3D<T>::operator()(eMatrix3D::eMatrix3DElements index) const
{
return *(m_elem + index);
}
/// Get matrix m_elem (non-const version)
/// \note 1-indexed
//
template <typename T>
T Matrix3D<T>::operator()(eMatrix3D::eMatrix3DElements index)
{
return *(m_elem +index);
}
【问题讨论】:
-
为什么不用两个索引来索引矩阵(
m_elem[i][j])?看来你还是想这样做。
标签: c++ multidimensional-array