Archived

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

Viri-

Passing an array paramater....

Recommended Posts

Viri-    122
Ok I have an array declared called pVertices. Actually its of type CFlag so its: CFlag pVertices[400]; I later want to pass this parameter to a function that access all the values of the class. So I have: SetupFlag(...., CFlag *pVertices); This only returns an error. How can I do this so that if the coder declares pVertices to [400] it can still be accessed by the other function? Thanks for any and all help....

Share this post


Link to post
Share on other sites
merlin9x9    174
Well, I'm not 100% sure about the particulars of your code, but here are two ways to pass arrays to functions:

  
void func1(int* array_ptr)
{
array_ptr[0] = 1;
array_ptr[1] = 12;
// whatever

}

void func2(int array[400])
{
array[0] = 1;
array[1] = 12;
// whatever

}

void main(void)
{
int myarray[400];

// these both do the same thing

func1(myarray);
func2(myarray);
}


Edited by - merlin9x9 on June 30, 2001 3:08:27 AM

Share this post


Link to post
Share on other sites
Viri-    122
Ok I was working with the NeXe tutorials and was workign with the Flag Tutorial. I changed a thing or two here and there and liked how it works and I''ve really wanted to have this effect saved so I could add it to my project any time I want. I have two files: Flag.h/.cpp And all is good. But then I changed some things around and now I get this error on my setup func:

...\main.cpp(56) : error C2664: ''SetupFlag'' : cannot convert parameter 5 from ''class CFlagVertex (*)[400]'' to ''class CFlagVertex *[]''
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

What I have in my code is this:
CFlag Flag;
CFlagVertex pVertices[400];
...
// the func in the class (flag.h/.cpp) is:
void SetupFlag(CDirect3D *pDirect3D, char *szTextName, CTexture *pCTexture, CVertexBuffer *pCVertBuff, CFlagVertex *pVertices[]);

// the func in my main.cpp
Flag.SetupFlag(&Direct3D, "nel.bmp", &pCTexture, &CVertBuff, &pVertices);

What the problem is is that I have pVertices declared at 400
(ie: pVertices[400] and I want to be able to let the coder decide how big the flag and be able to pass it to the setup func so it will setup the points correctly. But I again get the error: ''class CFlagVertex (*)[400]'' to ''class CFlagVertex *[]''

What am I doing wrong or what do I need to do to fix this? If this just isn''t clear let me know and I''ll post more code and try again explaining it.. basicly I''m just trying to pass pVertices[400] into my function: FlagSetup(...CFlagVertex *pVertices)

Thanks for any and all help...

Share this post


Link to post
Share on other sites
Oluseyi    2103
quote:
Original post by Viri-
// the func in the class (flag.h/.cpp) is:
void SetupFlag(CDirect3D *pDirect3D, char *szTextName, CTexture *pCTexture, CVertexBuffer *pCVertBuff, CFlagVertex *pVertices[]);



If you wish to pass a pointer to an array, as your main.cpp function call suggests, change that to:

void SetupFlag(CDirect3D *pDirect3D, char *szTextName, CTexture *pCTexture, CVertexBuffer *pCVertBuff, CFlagVertex **pVertices);

This makes your function expect a pointer to an array (or an array of pointers). Hope this helps

Share this post


Link to post
Share on other sites
Viri-    122
Nope still not working. But here is the error message:
c:\d3dprojects\main.cpp(56) : error C2664: ''SetupFlag'' : cannot convert parameter 5 from ''class CFlagVertex (*)[400]'' to ''class CFlagVertex ** ''

Here is my class:
(how do I do the code thing)
class CFlagVertex
{
public:
D3DXVECTOR3 position;
D3DCOLOR color;
FLOAT tu, tv;
};

here is the function header in .h file:
void SetupFlag(CDirect3D *pDirect3D, char *szTextName, CTexture *pCTexture, CVertexBuffer *pCVertBuff, CFlagVertex **pVertices);

And in the main.cpp file here is the function that calls it:
Oh and in main.cpp
CFlagVertex pVertices[400]; //is delcared in main.cpp
and the function call to the Flag part is:
Flag.SetupFlag(&Direct3D, "nel.bmp", &pCTexture, &CVertBuff, &pVertices);

So how can I get this to go through..??
Thanks

Share this post


Link to post
Share on other sites
Dean Harding    546
You could just cast it:

    
Flag.SetupFlag(&Direct3D, "nel.bmp", &pCTexture, &CVertBuff, (CFlagVertex **)&pVertices);



War Worlds - A 3D Real-Time Strategy game in development.

Edited by - Dean Harding on July 1, 2001 5:46:49 AM

Share this post


Link to post
Share on other sites
Viri-    122
Ok well tried that and it gets rid of the casting error and now I get this:

C:\D3DProjects\Main.cpp(57) : error C2248: ''SetupFlag'' : cannot access private member declared in class ''CFlag''

Ok so how can this be. I have these classes created:
flag.h
#ifndef __Flag_H_
#define __Flag_H_

#include
#include
#include
#include "Errorlogin.h"
#include "FlagVertex.h"


class CFlag
{

void SetupFlag(CDirect3D *pDirect3D, char *szTextName, CTexture *pCTexture, CVertexBuffer *pCVertBuff, CFlagVertex *pVertices);
void MakePoints( CFlagVertex *pVertices );


HRESULT d3dErr;
CErrorLog CError;

};

#endif

FlagVertex.h:
#ifndef __FlagVertex_H_
#define __FlagVertex_H_
#include
#include "Errorlogin.h"

class CFlagVertex
{
public:
D3DXVECTOR3 position;
D3DCOLOR color;
FLOAT tu, tv;
};
// our flags CustomVertex
#define D3DFVF_CFlagVertex (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
#endif

and then I have the same functions declared as before. What the heck private member could it be talking about?

Thanks for all help

Share this post


Link to post
Share on other sites
Viri-    122
Hey thanks a lot. I must be the dumbest coder ever.. kinda reminds me of the time I put a : instead of a ; and looked at the line for hours..

thanks!

Share this post


Link to post
Share on other sites