Archived

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

Brain Freeze -- Passing Arrays of Structs..

This topic is 5112 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

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.

Share this post


Link to post
Share on other sites
//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??





Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites