0
我有一個矩陣類,它有一個2d元素數組作爲成員變量。還有一個定製的ctor,其中包含2d數組作爲參數。奇怪的是當我嘗試測試它時,它會拋出錯誤C2440:錯誤C2440:'':無法從'float [3] [3]'轉換爲'Matrix'。下面的代碼:C++錯誤c2440使用帶2d參數的構造函數
Matrix.h
class Matrix
{
public:
Matrix();
~Matrix();
Matrix(const Matrix& other);
Matrix(float e_11, float e_12, float e_13,
float e_21, float e_22, float e_23, \
float e_31, float e_32, float e_33);
Matrix(float *elements[]);
private:
float* m_ElementsA; //< The array of elements of the vector
}
Matrix.cpp
#include "../include/Matrix.h"
Matrix::Matrix()
{
m_ElementsA = new float[9];
for (int i = 0; i < 9; ++i)
{
*(m_ElementsA + i) = 0.0f;
}
}
Matrix::~Matrix()
{
delete[] m_ElementsA;
}
Matrix::Matrix(const Matrix& other)
{
m_ElementsA = new float[9];
unsigned int row, column;
for (int i = 0; i < 9; ++i)
{
GetRowColumnFromIndex(i, row, column);
*(m_ElementsA + i) = other.GetElement(row, column);
}
}
Matrix::Matrix(float e_11, float e_12, float e_13, \
float e_21, float e_22, float e_23, \
float e_31, float e_32, float e_33)
{
m_ElementsA = new float[9];
*m_ElementsA = e_11;
*(m_ElementsA + 1) = e_12;
*(m_ElementsA + 2) = e_13;
*(m_ElementsA + 3) = e_21;
*(m_ElementsA + 4) = e_22;
*(m_ElementsA + 5) = e_23;
*(m_ElementsA + 6) = e_31;
*(m_ElementsA + 7) = e_32;
*(m_ElementsA + 8) = e_33;
}
Matrix::Matrix(float *elements[])
{
m_ElementsA = new float[9];
unsigned int row, column;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
*(m_ElementsA + (i * 3) + j) = elements[i][j];
}
}
}
的main.cpp
int _tmain(int argc, _TCHAR* argv[])
{
float arr[3][3] =
{
{1.1f, 2.2f, 3.3f},
{4.4f, 5.5f, 6.6f},
{7.7f, 8.8f, 9.9f}
};
Matrix mat3 = Matrix(arr);
return(0);
}
做什麼我在這裏做錯了嗎?提前致謝。
@BawenangRukmokoPardianPutra很高興我幫了忙。如果它解決了您的問題,請不要忘記將其標記爲已接受的答案。 – 2014-10-29 14:28:28
是的,我已經做到了。這是因爲幾分鐘的時間限制才接受我之前沒有做過的答案。順便說一句,如果可以的話,另一個問題如果你想傳遞任意長度的二維矢量,那麼你如何做到這一點? – 2014-10-29 14:56:10
由於這是標記爲C++,我會推薦使用std :: vector。無論如何,如果你真的想用C數組來實現它,你應該手動指定尺寸(http://stackoverflow.com/q/3991057/1938163) – 2014-10-29 14:58:34