Archived

This topic is now archived and is closed to further replies.

Something funny with a class member...

This topic is 6076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is the situation: I have a class called MESH. It has 3 member functions that compile fine. I also have 4 class members, but only 3 of the 4 compile fine. The one variable that is giving me trouble is a DWORD type with a name like ''NumMaterials''. The compiler keeps telling me that it needs a value because it is a const (which it isn''t). Has anyone had this happen before? How can it be fixed? I will get the code up here as soon as I get home (I am at school right now).

Share this post


Link to post
Share on other sites
Alright, here''s the code for the class:
  
//the Mesh class

class MESH
{
public:
//variables

LPD3DXMESH Mesh; //the mesh object

D3DMATERIAL8* MeshMaterials; //the pointer to the materials

LPDIRECT3DTEXTURE8* MeshTextures; //the textures for the mesh

DWORD &NumMaterials; //the number of materials in the mesh


//functions

MESH(LPSTR MeshName); //the constructor that loads the mesh

bool RenderMesh(); //the mesh rendering function

~MESH(); //the function that deletes the mesh


};

And here''s the functions:
  
//the constructor for the mesh function - it loads a mesh

MESH::MESH(LPSTR MeshName)
{
LPD3DXBUFFER pD3DXMtrlBuffer;

// Load the mesh from the specified file

if( FAILED( D3DXLoadMeshFromX( MeshName, D3DXMESH_SYSTEMMEM,
lpd3ddevice, NULL,
&pD3DXMtrlBuffer, &NumMaterials,
&Mesh ) ) )
{
MessageBox(hWnd, "Couldn''t load Mesh", "Error!", MB_OK);
}

// We need to extract the material properties and texture names from the

// pD3DXMtrlBuffer

D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
MeshMaterials = new D3DMATERIAL8[NumMaterials];
MeshTextures = new LPDIRECT3DTEXTURE8[NumMaterials];

for( DWORD i=0; i<NumMaterials; i++ )
{
// Copy the material

MeshMaterials[i] = d3dxMaterials[i].MatD3D;

// Set the ambient color for the material (D3DX does not do this)

MeshMaterials[i].Ambient = MeshMaterials[i].Diffuse;

// Create the texture

if( FAILED( D3DXCreateTextureFromFile( lpd3ddevice,
d3dxMaterials[i].pTextureFilename,
&MeshTextures[i] ) ) )
{
MeshTextures[i] = NULL;
}
}

// Done with the material buffer

pD3DXMtrlBuffer->Release();

}

//the rendering function for the mesh

bool MESH::RenderMesh()
{
// Meshes are divided into subsets, one for each material. Render them in

// a loop

for( DWORD i=0; i<NumMaterials; i++ )
{
// Set the material and texture for this subset

lpd3ddevice->SetMaterial( &MeshMaterials[i] );
lpd3ddevice->SetTexture( 0, MeshTextures[i] );

// Draw the mesh subset

Mesh->DrawSubset( i );
}

}

//the destructor - it cleans all the stuff up

MESH::~MESH()
{
if(MeshMaterials!=NULL)
delete[] MeshMaterials;

if(MeshTextures)
{
for( DWORD i = 0; i<NumMaterials; i++ )
{
if(MeshTextures[i])
MeshTextures[i]->Release();
}
delete[] MeshTextures;
}
if(Mesh != NULL)
Mesh->Release();
}

So does anyone have any idea why its doing that?

P.S. - the error is "D:\Stydx7in24\Source\lightworks\meshes.cpp(12) : error C2758: ''NumMaterials'' : must be initialized in constructor base/member initializer list"

Share this post


Link to post
Share on other sites
From my understanding, references are treated like const pointers. And since you said that it is going to change (NumMaterials), just use a DWORD* instead. I don''t think you are allowed to change the memory address of a reference just the value what it points to. Hope this helps.

Brendon

Share this post


Link to post
Share on other sites