Sign in to follow this  
johnnyBravo

C++, Ugggh what is wrong with this code!

Recommended Posts

Im doing a tutorial and the code below from it won't comile:
struct _VERTEX	{
	D3DXVECTOR3 pos;
	D3DXVECTOR3 norm;
	float tu;
	float tv;
}VERTEX,*LPVERTEX;

LPVERTEX pVerts;

The errors are:
Quote:
error C2146: syntax error : missing ';' before identifier 'pVerts' error C2065: 'pVerts' : undeclared identifier
Something to do with the 'VERTEX,*LPVERTEX;', any ideas? thx!

Share this post


Link to post
Share on other sites
You are declaring a _VERTEX pointer called *PVERTEX... then you are trying to instantiate a PVERTEX, which isn't a type (it's a variable).

That's like saying

int *intPtr;

intPtr x;

Share this post


Link to post
Share on other sites
_VERTEX is a type.
VERTEX is a variable of type struct _VERTEX
LPVERTEX is a variable of type struct _VERTEXµ

If you're trying to do a C struct type definition, you're missing a typedef keyword at the beginning, but in C++, they are considered poor style.

_VERTEX is a reserved identifier in C++ (underscore + capital letter)


struct VERTEX
{
D3DXVECTOR3 pos;
D3DXVECTOR3 norm;
float tu;
float tv;
};

typedef VERTEX* LPVERTEX;

Share this post


Link to post
Share on other sites
You're horribly mixing C and C++ idioms:
struct vertex
{
D3DXVector3 position;
D3DXVector3 normal;
float tu;
float tv;
};

vertex * vertices;
Σnigma

Share this post


Link to post
Share on other sites
I know this is d3d, but its general c++ errors im getting, so heres the full source, I tried using the
'typedef VERTEX* LPVERTEX;' on it, but I get:

Quote:

error C2664: 'ID3DXMesh::LockVertexBuffer' : cannot convert parameter 2 from 'BYTE **' to 'LPVOID *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast



struct VERTEX //removed _VERTEX
{
D3DXVECTOR3 pos; // vertex position
D3DXVECTOR3 norm; // vertex normal
float tu; // texture coordinates
float tv;
}; // removed VERTEX,*LPVERTEX;
typedef VERTEX* LPVERTEX; //added
#define FVF_VERTEX D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1

LPD3DXMESH CreateMappedSphere(LPDIRECT3DDEVICE8 pDev,float fRad,UINT slices,UINT stacks)
{
// create the sphere
LPD3DXMESH mesh;
if (FAILED(D3DXCreateSphere(pDev,fRad,slices,stacks,&mesh,NULL)))
return NULL;

// create a copy of the mesh with texture coordinates,
// since the D3DX function doesn't include them
LPD3DXMESH texMesh;
if (FAILED(mesh->CloneMeshFVF(D3DXMESH_SYSTEMMEM,FVF_VERTEX,pDev,&texMesh)))
// failed, return un-textured mesh
return mesh;

// finished with the original mesh, release it
mesh->Release();

// lock the vertex buffer
LPVERTEX pVerts;
if (SUCCEEDED(texMesh->LockVertexBuffer(0,(BYTE **) &pVerts))) {

// get vertex count
int numVerts=texMesh->GetNumVertices();

// loop through the vertices
for (int i=0;i<numVerts;i++) {

// calculate texture coordinates
pVerts->tu=asinf(pVerts->norm.x)/D3DX_PI+0.5f;
pVerts->tv=asinf(pVerts->norm.y)/D3DX_PI+0.5f;

// go to next vertex
pVerts++;
}

// unlock the vertex buffer
texMesh->UnlockVertexBuffer();
}

// return pointer to caller
return texMesh;
}



thx

Share this post


Link to post
Share on other sites
Quote:
Original post by rpg_code_master
*** Source Snippet Removed ***

All you worries will now be over [smile]


Nope, not all. _VERTEX is still a reserved name.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You have to add typedef before struct definition.

Share this post


Link to post
Share on other sites
Yup, you're missing a typedef. But try to stay away from declaring structs like that unless you're actually writing C code, not C++.

As for the second problem, change:
if (SUCCEEDED(texMesh->LockVertexBuffer(0,(BYTE **) &pVerts))) {
to:
if (SUCCEEDED(texMesh->LockVertexBuffer(0,(void **) &pVerts))) {

Share this post


Link to post
Share on other sites
Quote:
Original post by rpg_code_master
*** Source Snippet Removed ***

All you worries will now be over [smile]


:) Nearly over

I'm getting
Quote:

error C2146: syntax error : missing ';' before identifier 'pVerts'
error C2065: 'pVerts' : undeclared identifier


from: '(BYTE**)&pVerts'


sphere->LockVertexBuffer(0, (BYTE**)&pVerts);





Share this post


Link to post
Share on other sites
Replace your lock vertex buffer line with this:


if (SUCCEEDED(texMesh->LockVertexBuffer(0,reinterpret_cast<BYTE **> &pVerts))) {

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this