Brain Freeze -- Passing Arrays of Structs..

Started by
7 comments, last by OpenGL_Guru 20 years, 4 months ago
ok i know this is easy but for some reason i have a brain freeze, i think its the 5 hours sleep i got last night... struct array { char string[30]; int length; }; array myArray[20]; ok so i want to pass myArray to a function in another file thats been included in the main.cpp file. there in this function i will assign a string to each myArray element and this will update(arrays are always reference) in the main. only problem is is that the same type struct has to be declared in this other file , but you cant redeclare the same type more than once. i know this is stupid , but i cant think of what to do. been a while since i worked with arrays of structs.. i am using Visual C++ 6.0.
heh
Advertisement
Forward declare your struct in the other file, such as:

struct array;
Or use a typdef like:

struct _array
{
char string[30];
int length;
};
typedef _array array

array myArray[20];
- growl -
//in menu.h

struct Array;

void setup_menu(Array myMenu[30])
{

strcpy(myMenu[0].string, "hello");

}

//in MAIN.CPP

struct Array
{
char string[30];
int length;
};

Array arrayMenu[20];


//function call

setup_menu(arrayMenu);
******************************

errors:

c:\program files\microsoft visual studio\vc98\myprojects\ascii_2_3d\menu.h(12) : error C2036: ''struct Array []'' : unknown size
c:\program files\microsoft visual studio\vc98\myprojects\ascii_2_3d\menu.h(12) : error C2027: use of undefined type ''Array''
c:\program files\microsoft visual studio\vc98\myprojects\ascii_2_3d\menu.h(5) : see declaration of ''Array''
c:\program files\microsoft visual studio\vc98\myprojects\ascii_2_3d\menu.h(12) : error C2228: left of ''.string'' must have class/struct/union type
Error executing cl.exe.

ascii_2_3D.exe - 3 error(s), 1 warning(s)

...ok so why is it barfing all over me??





heh
Try this:

void setup_menu(Array *myMenu)
{
..
}

--
You''re Welcome,
Rick Wong
- sitting in his chair doing the most time-consuming thing..
i still get the same error(s).
hmmm this is really crazy!!!

Compiling...
ascii_2_3D.cpp
c:\program files\microsoft visual studio\vc98\myprojects\ascii_2_3d\texture_bmp.h(63) : warning C4018: ''<'' : signed/unsigned mismatch
c:\program files\microsoft visual studio\vc98\myprojects\ascii_2_3d\menu.h(12) : error C2036: ''struct Array *'' : unknown size
c:\program files\microsoft visual studio\vc98\myprojects\ascii_2_3d\menu.h(12) : error C2027: use of undefined type ''Array''
c:\program files\microsoft visual studio\vc98\myprojects\ascii_2_3d\menu.h(5) : see declaration of ''Array''
c:\program files\microsoft visual studio\vc98\myprojects\ascii_2_3d\menu.h(12) : error C2228: left of ''.string'' must have class/struct/union type
Error executing cl.exe.

ascii_2_3D.exe - 3 error(s), 1 warning(s)
heh
Why is the function definition in the header file instead of only the declaration?
Well, R2D22U2..
yes im sorry for the hassle, i moved the implementation of the header to a menu.cpp and left the definition of the struct in to where both main.cpp and menu.cpp could see it. and it worked fine. thanks anyway for your help!

--errr wonder why it still croaked at me though, thought there was a way to get past those set of errors. seems like i have before.
heh
quote:Original post by OpenGL_Guru
yes im sorry for the hassle, i moved the implementation of the header to a menu.cpp and left the definition of the struct in to where both main.cpp and menu.cpp could see it. and it worked fine. thanks anyway for your help!

--errr wonder why it still croaked at me though, thought there was a way to get past those set of errors. seems like i have before.


As I said before "void setup_menu(Array myMenu[30])" should have been in its own source file and of course you want the struct to be placed in a header file that you would include in both the main.cpp and menu.cpp. Since you did that eventually, it now works.

[edited by - nervo on December 16, 2003 3:55:46 PM]
Well, R2D22U2..

This topic is closed to new replies.

Advertisement