引用全局结构中的对象

时间:2014-01-06 21:31:32

标签: c++

我有一个简单的矩阵和矢量类:

class Matrix
{
public:
    Matrix(unsigned nrows = 1, unsigned ncols = 1);
    virtual ~Matrix() {} 

    //operators
    virtual float& operator() (unsigned row, unsigned col); 
    virtual float  operator() (unsigned row, unsigned col) const;

    ... other stuff ...

protected:
    unsigned mRows;
    unsigned mCols;
    vector<float> MatrixData;
};    

// vector class and methods ( n x 1 matrix)
class VectorM: public Matrix
{
public:
    VectorM(unsigned nRows = 1);
    virtual ~VectorM(){} // Destructor

    //operators
    float& operator() (unsigned row); 
    float  operator() (unsigned row) const;
};

我在嵌入式系统上运行此代码,因此我希望它尽可能快地运行。在使用这些对象的函数中,不是让对象本地化(因此每次函数运行时都创建和销毁它们),我想把它们放到一个定义为

的全局结构中。
typedef struct {
    VectorM dQuat(4);
    VectorM aQuat(4);
    VectorM attErr(3);
    VectorM ex_hat(3); 
    VectorM ex_d(3);
    VectorM ez_d(3);
    VectorM v(3); 
    VectorM ey_p(3);
    VectorM ez_p(3);

    Matrix Rvb_hat(3,3);
    Matrix Rvb_d(3,3); 
    Matrix RZ(3,3);
    Matrix v_hat(3,3);
    Matrix Rp(3,3);
    Matrix I(3,3);

}tRTT;

并实例化和引用(我知道函数没有必要,但我与现有代码保持一致)

tRTT gRTT;
tRTT *GetRTTPointer(void)
{
    return(&gRTT);
}

使用该结构的函数是

void StepInnerLoop()
{
    ... stuff ...
    float bodyError[3];
    tRTT *RTT = GetRTTPointer();

    // these appear to be fine
RTT->dQuat(1) = desQuat[0]; RTT->aQuat(1) = attitudeQuat[0];
RTT->dQuat(2) = desQuat[1]; RTT->aQuat(2) = attitudeQuat[1];
RTT->dQuat(3) = desQuat[2]; RTT->aQuat(3) = attitudeQuat[2];
RTT->dQuat(4) = desQuat[3]; RTT->aQuat(4) = attitudeQuat[3];

ResolvedTiltTwist(RTT);

bodyError[0] = RTT->attErr(1); // ERROR #1 HERE
bodyError[1] = RTT->attErr(2);
bodyError[2] = RTT->attErr(3);   

... other stuff ...
}

void ResolvedTiltTwist(tRTT *RTT)
{
    float thetaTilt, thetaSign;

    Quat2DirectionCosine(RTT->Rvb_hat,RTT->aQuat); //ERROR #2 HERE
    RTT->ex_hat(1) = RTT->Rvb_hat(1,1); 
    RTT->ex_hat(2) = RTT->Rvb_hat(1,2); 
    RTT->ex_hat(3) = RTT->Rvb_hat(1,3); 

    ...stuff...
}

void Quat2DirectionCosine(Matrix& dc, const VectorM& e)
{
   ... stuff ...
}

我得到两个错误 - 错误#1是没有合适的转换函数从“VectorM”到“float”存在而错误#2是指向绑定函数的指针只能用于调用函数。

如果在StepInnerLoop或ResolvedTiltTwist函数中声明了所有对象,那么一切都很好。有什么想法吗?

提前致谢。

0 个答案:

没有答案