提问者:小点点

从二维数组返回元素(类型错误)


但是,我试图为我的2D数组类Matrix3D编写一个访问器,编译器给了我一些C风格代码的问题,我希望这些代码在这种情况下能够很好地工作。错误为错误C2440:“return”:使用我的访问器时无法从“const T[3]”转换为“const T”:const T运算符()(Ematrix3D::Ematrix3Delements)const T运算符()(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);
}

共1个答案

匿名用户

既然您已经使用了枚举(顺便说一句,考虑使用枚举类)作为索引,并且存储是私有的,那么为什么不使用一维数组/向量呢?

如果退一步说,您基本上希望使用诸如Matrix3d.get(1,1)之类的索引访问坐标E11等。这里的映射是微不足道的。那么人们通常希望使用单维结构,并将索引i,j转换为类似于i*;+J,以仍然保持2D接口。现在,您已经希望使用单个索引访问它,该索引是通过enum提供的。这实际上已经为你做了有意义的翻译,所以我看不出为什么你会在内部使用2D结构。只会让事情变得更复杂。

如果您确实希望在内部保持2D,则需要使用除法和模将1D枚举索引转换回有意义的2D索引,或者基于它硬编码switch-case

此外,我宁愿使用运算符[]或专用的at()方法,而不是运算符(),因为这通常与函数调用和构造相关联(构造函数是对构造函数的函数调用)。从长远来看,这可能会使您和Matrix3D的用户感到困惑。