![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
help : error C2065: 'information' : undeclared identifierThis is going to be alot of code in this post lol. Right basically I have only now got used to header files in C, but im having problems with myLib.cpp. Here are the errors that are being displayed.
Code:
Now here is my header file callwed myLib.h CPP / C++ / C Code:
here is my cpp file called myLib.cpp CPP / C++ / C Code:
and at last here is my main code CPP / C++ / C Code:
Now what i am trying to do is actually for the first time use a header file with cpp file to make the main as little as possible. So i can print the structure and create the structure with my own functions. but i keep getting the errors, Can someone have a quick skim through my code and try to target the problem areas. I am realtivly new to programming and have tried for hours to fix the errors with no luck. Thanks in advanced |
|
#2
|
|||
|
|||
Re: help : error C2065: 'information' : undeclared identifierQuote:
I believe that the Microsoft compilers ignore everything before the #include "stdafx.h" statement. Suggestion #1: Try again with #include "stdafx.h" as the very first line in whatever files use it. Suggestion #2: Until you really get into situations where compile time is an issue, I suggest that you see if you can make projects that don't include stdafx.h. This requires a change from the default project settings. The choice not to use precompiled headers is somewhere in your projects->properties menu, I think. In Visual C++ 2005 express it can be reached as follows: Right-click on the project name in the "Solution" panel, then from the pop-up context menu select properties->Configuration Properties->C/C++->Precompiled HeadersThe top item in the box that opens up is "Create/Use Precompiled Headers" You should be able to select "Not Using Precompiled Headers". (And, of course, delete the #include "stdafx.h" statements in your source file(s).) It might be somewhat different for other versions of compiler. I personally recommend the second one, since many people who follow these threads might like to compile your programs and they are not using a Microsoft compiler with stdafx.h. (That might help some beginners, and it also might help people who are trying to help you.) Regards, Dave |
|
#3
|
|||
|
|||
Re: help : error C2065: 'information' : undeclared identifierThanks a hell of a alot. Really helped me out. Once i cleared those errors i could guid my self from other errors
Code:
I dotn have a clue what i t means. Any advice would be great. Ill post my new code again just incase its something in there. (The main is very small now myLib.h CPP / C++ / C Code:
myLib.cpp CPP / C++ / C Code:
main CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: help : error C2065: 'information' : undeclared identifierQuote:
Each source file is compiled as a separate "compilation unit". As each file is compiled, an "object file" is created that holds information about the code and all of the variables that were defined. To create an executable file, the linker puts together everything from the object files. So Here's the Poop: 1. myLib.cpp is compiled. One of the things that happens is that a variable named customers is created. (An array of information thingies.) Storage and linkage information for the array customers is put into myLib.obj, along with the executable code from myLib.cpp 2. The main (worksheet4.cpp) file is compiled and creates its own variable named customers, which is an array of information things. The variable linkage and code from worksheet4.cpp is stored in an object file, named worksheet4.obj 3. The linker tries to tie together the code and variables in the two object files and finds that the variable customers is defined more than once. Thus the error message. The solution is to refrain from putting any actual storage allocation in a header file that will be used in more than one place. Note that your inclusion guard definition in the header file protects it from being included more than once in a given compilation unit, but can not prevent the kind of error that you have seen: including it in more than once in different source files is a no-no since it actually allocates storage for a variable. (Even though both files are in the same project for your Microsoft Visual C++ installation, the individual files are compiled separately, as required by the standard C and C++ language definitions.) One possible solution: 1. Change the header file so that the variable is declared extern. myLib.h CPP / C++ / C Code:
2. Then in one (and only one) of the source files. (I vote for the one with main() in it) put the actual allocation statement CPP / C++ / C Code:
Now the variable is a global and can be easily used in any file that includes "myLib.h" A better solution is to not make the variable a global. Declare it in main(), and define any functions that need to use it have a parameter list that allows the variable to be passed as an argument. There are lots of reasons to avoid using global variables as a general programming practice. It is not actually "illegal" to have them, but is highly dis-recommended at this point in your development. I showed you how to use globals if you really need them, not because I think you should use them, but in hopes of helping you understand the overall compile/link process that takes place when you build a program. In fact, you have already done most of what I just recommended. Your functions setinfo() and readinfo() have parameters named "customers" and code inside those functions will access the value of the "customers" parameter passed to them as an argument. (So they wouldn't even see the global "customer" variable anyhow.) Bottom line: 1. Remove the "customers" declaration from myLib.h. 2. Put the "customers" inside the main() function. Just after the opening brace "{" of the main() function would be as good a place as any. Regards, Dave |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Fraction program | SpudNuts | C++ Forum | 6 | 25-Sep-2006 03:50 |
| Help with syntax errors | PeteGallo | C Programming Language | 7 | 08-Aug-2005 20:30 |
| Can enum have same name as class? | crystalattice | C++ Forum | 3 | 08-Dec-2004 16:43 |
| Computer privacy article | crystalattice | Open Discussion Forum | 0 | 01-Oct-2004 14:26 |
| Help! undeclared identifier error | wfillis | .NET Forum | 8 | 25-Aug-2004 06:03 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The