References in initializer list
I have a camera class with two reference variables. I want to pass in two references through the constructor and assign the reference variables to these, so that if I change these in this class, the other ones I defined before (and passed in) will change too. However, I get the error:
a reference of type "D3DXMATRIX &" (not const-qualified) cannot be initialized with a value of type "D3DMATRIX"
and
error C2440: 'initializing' : cannot convert from 'D3DMATRIX' to 'D3DXMATRIX &'
here is my code:
header:
#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
source:
#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));
}
Obviously I am misunderstanding something fundamental. Any help would be greatly appreciated!
The errors I get are on the initialiser list in the constructor.
here is where I am instantiating the camera:
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;
}
It seems to me you are trying to initialize reference to Derived with a reference to Base, as in:
class D3DMATRIX {};
class D3DXMATRIX : public D3DMATRIX {};
class Camera {
private:
D3DXMATRIX& m_;
public:
Camera(D3DMATRIX& m) : m_(m) {}
};
MSVC9.0 says:
test.cpp(9) : error C2440: 'initializing' : cannot convert from 'D3DMATRIX' to 'D3DXMATRIX &'
Maybe you should have Camera constructor taking D3DXMATRIX& as parameters?
The problem is D3DXMATRIX derives from D3DMATRIX. You can't store a reference to a D3DMATRIX as a D3DXMATRIX.
So, either pass in D3DXMATRIX in the first place, or store D3DMATRIX instead. Here's a trivial example that also doesn't work:
class A
{
};
class B : public A
{
};
class C
{
public:
C(A& a) : MyB(a) {}
private:
B& MyB;
};
You didn't show us where you instantiate Camera
, or any line numbers.
But you're probably trying to bind a temporary to those non-const
reference parameters in your constructor.
Why not store a const D3DXMATRIX&
, or else make a copy.
精彩评论