2013-09-05 30 views
1

我想使用類似如何在多維數組中正確使用qRegisterMetaType?

typedef double Matrix[4][4]; 

代表轉換,並且還與QT信號/槽機制,通過他們周圍。但是當我使用

Q_DECLARE_METATYPE(Matrix) 

它在這個函數

void *qMetaTypeConstructHelper(const T *t) 
{ 
    if (!t) 
     return new T(); 
    return new T(*static_cast<const T*>(t)); 
} 

拋出qmetatype.h一個錯誤說: 「錯誤C2075: '目標的operator new()的':數組初始化需要大括號」

回答

2

Q_DECLARE_METATYPE(T)要求T類型是默認構建的,可複製的和可破壞的。您的Matrix類型不可複製,因此您無法在其上使用Q_DECLARE_METATYPE

解決方法:使用一個類。

// copiable, C++98 brace-initializable, etc. 
struct Matrix { 
    double data[4][4]; 
}; 
1

理想情況下,您應該使用eigen3及其類型。或者,將你的類型包裝在一個類中。一旦你這樣做了,你不妨做的只不過是一個包裝。最終,你會發現eigen3是唯一可行的方法。可能當你到達這一點時:

#include <cstring> 

class Matrix { 
    double m_data[4][4]; 
public: 
    typedef double (*Data)[4]; 
    Matrix() {} 
    Matrix(const Matrix & other) { memcpy(m_data, other.m_data, sizeof m_data); } 
    Matrix & operator=(const Matrix & other) { memcpy(m_data, other.m_data, sizeof m_data); return *this; } 
    Matrix & operator=(const Data other) { memcpy(m_data, other, sizeof m_data); return *this; } 
    operator Data() { return m_data; } 
}; 

int main() 
{ 
    double mat1[4][4]; 
    Matrix mat2; 
    mat2[3][3] = 1; 
    mat2 = mat1; 
    return 0; 
}