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

Started by
9 comments, last by gonzo 17 years, 11 months ago
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!
Advertisement
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;
_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;
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You're horribly mixing C and C++ idioms:
struct vertex{	D3DXVector3 position;	D3DXVector3 normal;	float tu;	float tv;};vertex * vertices;
Σnigma
typedef struct _VERTEX	{	D3DXVECTOR3 pos;	D3DXVECTOR3 norm;	float tu;	float tv;}VERTEX,*LPVERTEX;LPVERTEX pVerts;


All you worries will now be over [smile]
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_TEX1LPD3DXMESH 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
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You have to add typedef before struct definition.
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))) {
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);



This topic is closed to new replies.

Advertisement