Thread View: comp.os.msdos.programmer
2 messages
2 total messages
Started by Mike Barnard
Sat, 09 Mar 1996 02:06
SUUMMARY - my structure and pointer problems cured.
Author: Mike Barnard
Date: Sat, 09 Mar 1996 02:06
Date: Sat, 09 Mar 1996 02:06
82 lines
3802 bytes
3802 bytes
Hi. I recently posted about a function that would not recognise a structure and it's variables. Thanks to all who replied, and I have the problem fixed now. I hope this information may be of some use. Especially to newbies like me who don't know about these sorts of facts. The intention... Using Borland TC3, I had 3 functions. "Main", "Vpcmenu" and "Prntitem". "Main" printed a title box then called "vpcmenu" to display and get input about a main menu. This declared a structure to hold information about the individual menu items. I delared an array of 9 structures and a pointer to them, then initialised them with thier data. In a loop I passed the value of the pointer to the function "prntitem", whose job was to print the contents of the pointed-to structure on the screen in the correct place. The problem... I had a compiler error in the function "prntitem" where I tried to use the variables within the structure. The error said the variables were undeclared. I was also told that the pointer I passed was never used. I tried moving the structure declaration to global and just inside main but no dice; the function "prntitem" didn't recognise the structure. The answer... It was because I was using seperate source files for each of the functions. I assumed that because the structure had been defined 'upwind' of the function that wanted to use it the compiler would just take the data from each file, make a program and the scope would be OK . Oh, no! In Borland at least (I don't know about other compilers) structure definitions, includes and god only knows what else is only valid for the source file it is in. Split into multiple source files and you have to repeat the declarations. The cure... Creating a header file with the information in it and including it in each and every source file. The following is a better explanation cut from a reply... my thanks to "tljones1" and all the others who replied. ----------------------------------------------------------- > files shouldn't matter? The compiler should sort that out shouldn't > it? Maybe not... nope... the compiler won't sort things out for you. What is defined in one source file isn't in another. Believe it or not, but it keeps you from running into problems. You have to explicitly declare your types in each source file you intend to use them in. and the best way to do that is to have a include file that declares that.. For example mystuff.h would have all of the declarations for the source file mystuff.cpp (so it has all of the structure declarations, global variables, function prototypes, etc.) and mystuff.cpp would include mystuff.h (it is also standard that mystuff.h has all of the other inncludes that mystuff.cpp would need). Also another other source file that needed to use the types declared in mystuff.h would also include mystuff.h I hope this makes sense... take it easy... ----------------------------------------------------------- If anything I've said is inaccurate, please publicly correct me. But this has worked for me. Now for the next bit, getting input and checking for specific keypresses. (Arrows, numbers and enter)... sigh. I'll get there in the end. With the help of some friends. Thanks. --- Mic. From windy, damp, but clearing skies Worthing; England. mikebarnard@mistral.co.uk
Re: SUUMMARY - my structure and pointer problems cured.
Author: Jurgen Schwietering
Date: Sat, 09 Mar 1996 18:23
Date: Sat, 09 Mar 1996 18:23
13 lines
1172 bytes
1172 bytes
Mike Barnard (mikebarnard@mistral.co.uk) wrote:
: It was because I was using seperate source files for each of
: the functions. I assumed that because the structure had been
: defined 'upwind' of the function that wanted to use it the
: compiler would just take the data from each file, make a
: program and the scope would be OK . Oh, no! In Borland at
: least (I don't know about other compilers) structure
^^^^^^^^^^^^^^^^^^^^^
: definitions, includes and god only knows what else is only
: valid for the source file it is in. Split into multiple
: source files and you have to repeat the declarations.
Every compiler... ;-)
Jurgen
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads