2011-10-14 32 views
1

我有一個帶兩個參考變量的相機類。我想通過構造函數傳遞兩個引用並將引用變量賦值給這些變量,這樣如果我在這個類中更改這些變量,那麼我之前定義的其他變量也會發生變化。但是,我得到的錯誤:初始化程序列表中的參考文獻

a reference of type "D3DXMATRIX &" (not const-qualified) cannot be initialized with a value of type "D3DMATRIX" 

error C2440: 'initializing' : cannot convert from 'D3DMATRIX' to 'D3DXMATRIX &' 

這裏是我的代碼:

頭:

#ifndef CAMERA_H 
#define CAMERA_H 

#include <d3d10.h> 
#include <d3dx10.h> 
#include "globals.h" 
#include "direct3D.h" 

class Camera 
{ 
private: 
D3DXMATRIX &matProjection, &matView; 
public: 
Camera(
    float fOVDeg, float nearCull, float farCull, 
    float xPos, float yPos, float zPos, 
    D3DMATRIX &matProjection, D3DMATRIX &matView); 
void SetCamera(float fOVDeg, float nearCull, float farCull); 
void AdjustCamera(float x, float y, float z); 
}; 

#endif 

來源:

#include "Camera.h" 

Camera::Camera(
float fOVDeg, float nearCull, float farCull, 
float xPos, float yPos, float zPos, 
D3DMATRIX &matProjection, D3DMATRIX &matView) 
: matProjection(matProjection), matView(matView) 
{ 
this->SetCamera(fOVDeg, nearCull, farCull); 
this->AdjustCamera(xPos, yPos, zPos); 
} 

// Set the fixed properties of the 3D camera 
void Camera::SetCamera(float fOVDeg, float nearCull, float farCull) 
{ 
// create a projection matrix 
D3DXMatrixPerspectiveFovLH(
    &matProjection, 
    (float)D3DXToRadian(fOVDeg), // the horizontal field of view 
    (FLOAT)SCREEN_WIDTH/(FLOAT)SCREEN_HEIGHT, // aspect ratio 
    nearCull, // the near view-plane 
    farCull); // the far view-plane 
} 

// Set the adjustable properties of the 3D camera 
void Camera::AdjustCamera(float x, float y, float z) 
{ 
D3DXMatrixLookAtLH(&matView, 
        &D3DXVECTOR3 (x, y, z), 
        &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), 
        &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); 
} 

顯然我誤解了一些根本性的東西。任何幫助將不勝感激!

我得到的錯誤是在構造函數的初始化列表中。

這裏是我實例化攝像頭:

Camera* camera; 
D3DMATRIX matProjection, matView; 

//called once 
void Initialise(HWND hWnd) 
{ 
initD3D(hWnd); 
    init_pipeline(); 
cube = new Cube(); 

level = new Level(*cube); 

camera = new Camera(
    45.0f, 1.0f, 10000.0f, 
    0.0f, 9.0f, 100.0f, 
    matProjection, matView); 

test = 0.0f; 
} 
+0

編譯器會告訴你這行中出現的錯誤。 –

+0

在實例化一個'Camera'對象的地方顯示相關的代碼。 –

回答

3

在我看來,你正試圖初始化參考使用,以基礎參考派生,如:

class D3DMATRIX {}; 
class D3DXMATRIX : public D3DMATRIX {}; 

class Camera { 
private: 
    D3DXMATRIX& m_; 

public: 
    Camera(D3DMATRIX& m) : m_(m) {} 
}; 

MSVC9.0說:

test.cpp(9) : error C2440: 'initializing' : cannot convert from 'D3DMATRIX' to 'D3DXMATRIX &' 

也許你應該有相機的構造以D3DXMATRIX &爲參數?

+0

['D3DMATRIX'](http://msdn.microsoft.com/en-us/library/windows/desktop/bb172573(v = vs .85).aspx)和['D3DXMATRIX'](http://msdn.microsoft.com/en-us/library/windows/desktop/bb172912(v = vs.85).aspx)似乎無關快速谷歌檢查。 –

+0

[DirectX reference](http://msdn.microsoft.com/en-us/library/windows/desktop/bb172912%28v=vs.85%29.aspx)說他們是... – MichalR

+0

Ooops ... I只讀頂部的原始定義(可能是C版本)。 D3D中的C和C++數據類型的定義似乎有些不一致......我很高興我不需要去戰鬥那些:) –

1

你沒告訴我們你實例Camera,或行號。

但是,您可能正試圖將臨時綁定到您的構造函數中的那些非const引用參數。

爲什麼不存儲const D3DXMATRIX&,或者複製一份。

+0

aha ..打我吧...我會刪除我的.. – Nim

2

問題是D3DXMATRIX派生自D3DMATRIX。您不能將D3DMATRIX的引用存儲爲D3DXMATRIX。

因此,要麼首先傳入D3DXMATRIX,要麼存儲D3DMATRIX。下面是一個也不起作用一個簡單的例子:

class A 
{ 

}; 

class B : public A 
{ 

}; 

class C 
{ 
public: 

    C(A& a) : MyB(a) {} 

private: 

    B& MyB; 
}; 
+0

看起來,這兩種類型不是通過繼承相關的,從文檔:['D3DMATRIX'](http://msdn.microsoft.com/en-us/ library/windows/desktop/bb172573(v = vs.85).aspx)和['D3DXMATRIX'](http://msdn.microsoft.com/en-us/library/windows/desktop/bb172912(v = vs。 85).aspx) –

+0

看看代碼,而不是文檔:typedef struct D3DXMATRIX:public D3DMATRIX – Robinson

+0

對不起,C和C++似乎有兩種不同的定義...(ouch) –