Hi all, noob here. I’m trying to run a program in Dev-C++ that was created in Turbo C++. I already changed a few things, like updating the header files and replacing clrscr() with fflush, but it gives me a runtime error which causes the program to close (although the program is along, so I won’t post all of it here). There are also several compile errors, which I placed in comments on the line where they appear in the code:
Does anyone know how to fix these? Here is the code for the function containing the errors:
Hope this isn’t too ugly...sorry it's so long. This is my first time posting here, and I tried not to appear as a big artard. Thanks!
Oct 17, 2015 Such an error is shown when the compiler can't find the file which your are trying to include. In all probability, you are trying out a program written for the Turbo. In C, an identifier is expected in the following situations: in a list of parameters in an old-style function header; after the reserved words struct or union when the braces are not present, and; as the name of a member in a structure or union (except for bit fields of width 0). In C, an identifier is also expected in these situations.
It's your first C (or C++) program--it's not that long, and you're about tocompile it. You hit compile (or enter the build command) and wait. Yourcompiler spits out fifty lines of text. You pick out words like 'warning and'error'. Does that mean it worked? you wonder. You look for theresulting executable. Nothing. Damn, you think, I guess I haveto figure out what this all means...The Types of Compilation Errors
First, let's distinguish between the types of errors: most compilers will givethree types of compile-time alerts: compiler warnings, compiler errors, andlinker errors.Although you don't want to ignore them, compiler warnings aren't somethingsevere enough to actually keep your program from compiling. Usually, compilerwarnings are an indication that something might go wrong at runtime. How canthe compiler know this at all? You might be making a typical mistake that thecompiler knows about. A common example is using the assignment operator ('=')instead of the equality operator (') inside an if statement. Your compilermay also warn you about using variables that haven't been initialized and othersimilar mistakes. Generally, you can set the warning level of your compiler--Ilike to keep it at its highest level so that my compiler warnings don't turn into bugs in the running program ('runtime bugs').
Nevertheless, compiler warnings aren't going to stop you from getting yourprogram working (unless you tell your compiler to treat warnings as errors), sothey're probably a bit less frustrating than errors. Errors are conditionsthat prevent the compiler from completing the compilation of your files.Compiler errors are restricted to single source code files and are the resultof 'syntax errors'. What this really means is that you've done something thatthe compiler cannot understand. For instance, the statement 'for(;)' isn'tcorrect syntax because a for loop always needs to have three parts. Althoughthe compiler would have expected a semicolon, it would also have expected aconditional expression, so the error message you get might be something like'line 53, unexpected parenthesis ')'. Note, also, that compiler errors willalways include a line number at which the error was detected.
Even if you make it through the compilation process successfully, you may runinto linker errors. Linker errors, unlike compiler errors, have nothing to dowith incorrect syntax. Instead, linker errors are usually problems withfinding the definitions for functions, structs, classes, or global variablesthat were declared, but never actually defined, in a source code file.Generally, these errors will be of the form 'could not find definition for X'.
Usually, the compilation process will begin with a series of compilererrors and warnings and, once you've fixed all of them, you'll then be facedwith any linker errors. In turn, I'll first cover dealing with compiler errorsand then with linker errors.
Compiler Errors - Where do you start?
If you're faced with a list of fifty or sixty error and warning messages, itcan be daunting to even try to figure out where to start. The best place,though, is at the beginning--as in, the beginning of the list. In fact, youshould almost never start trying to fix errors from the end of the file to thebeginning for one simple reason: you don't know if they're actually errors!A single error near the top of your program can cause a cascade of othercompiler errors because those lines might rely on something early in theprogram that the compiler couldn't understand. For instance, if you declare avariable with improper syntax, the compiler will complain about that syntaxerror and that it cannot find a declaration for the variable. Leavingoff a semicolon in the wrong place can result in an astonishing number oferrors. Things like this can happen because C and C++ syntax allows for thingslike declaring of a type immediately after the type definition:This would create a variable, myStruct, with room to store a struct containingtwo integers. Unfortunately, this means that if you leave off a semicolon, thecompiler will interpret it as though the next thing in the program is intendedto be a struct (or return a struct). Something like thiscan result in an surprising number of errors (possibly including a complaintabout an extraneous 'int' being ignored). All this for a single character!best to start at the top.
Dissecting an Error Message
Most messages from the compiler will consist of at least four things: the typeof message--warning or error--source code file in which the error appeared, andthe line of the error, and a brief description of what was wrong. Output fromg++ for the above program might look something like this (your results withother compilers may vary):foo.cc is the name of the file. 7 is the line number in question, and it isclear that this is an error. The brief message here is quite helpful becauseit says exactly what was wrong. Notice, however, that the message makes senseonly in the context of the program. It doesn't say which struct was missing asemicolon.More cryptic was another error message from the same compilation attempt:'extraneous 'int' ignored'. It's up to the programmer to figure out exactlywhy it was extraneous. Notice again that this was an error caused by a problemearlier in the program, not on line 8, but earlier, when the struct lacked asemicolon terminator. Fortunately, it's pretty clear that the functiondefinition for foo was OK; this tells us that the error must have been causedsomewhere else in the program. In fact, it had to be earlier in theprogram--you won't get an error message that indicates a syntax error prior tothe line on which the error actually occurred.
This brings up another guiding principle of hunting down compiler errors: whenin doubt, look earlier in the program. Since syntax errors can havemysterious repercussions later, it's possible that the compiler was giving aline number that doesn't actually have a syntax error! Worse, many times, thecompiler won't be as friendly in telling you exactly what happened earlier inthe program. Even the first compiler error you get might be due to somethingseveral lines before the indicated warning.
Handling Cryptic or Bizarre Messages
There are several types of compiler errors that are especially frustrating.The first is the case of an undeclared variable that you swear you declared.Often times, you can actually point out exactly where the variable wasdeclared! The problem is often that the variable is simply misspelled.Unfortunately, this can be very hard to see since the mind typically reads whatit expects rather than what is actually there. Worse, there are other reasonswhy this could be a problem too--scoping issues for instance!To sort through the possible problems, one trick I like to use is to go to theline of the supposedly undeclared variable and have my text editor perform asearch for the word under the cursor (alternatively, you could copy thevariable name and perform a search); this guarantees that if I spelled itincorrectly, it will not find a match for my search. This also keeps me fromhaving to type the word, which could result in my correctly spelling thevariable name.
A second cryptic message is the 'unexpected end of file'. What's going onhere? Why would the end of the file be 'unexpected'? Well, the key here is tothink like the compiler; if the end of the file is unexpected, then it must bethat it's waiting for something. What could it be waiting for? The answer isusually 'closure'. For instance, closing curly braces or closing quotes. Agood text editor thatperforms syntax highlighting and automatic indentation should help fix some ofthese issues by making it easier to spot problems when writing code.
Ultimately, when a message is cryptic, the way to approach the problem is tothink about how the compiler is trying to interpret the file. This can be hardwhen you're just starting out, but if you pay attention to the messages and tryto pick out what they could mean, you'll quickly get used to the generalpatterns.
Finally, if nothing else works, you can always just rewrite a fewlines of code to clear out any hidden syntax errors that might be hard for theeye to catch. This can be dangerous if you don't end up rewriting the rightsection of code, but it can be helpful.
Linker Errors
Once you've finally cleaned up all those frustrating syntax errors, taken anap, had a meal or two, and mentally prepared yourself for the program to buildcorrectly, you may still need to deal with linker errors. These can often bemore frustrating because they aren't necessarily the result of somethingwritten in your program. I'll briefly cover some of the typical types oflinker errors you can expect and some of the ways to fix them.You may have issues with how you set up your compiler. Forinstance, even if you include the correct header files for all of yourfunctions, you still need to provide your linker with the correct path to thelibrary that has the actual implementation. Otherwise, you will get 'undefinedfunction' error messages. Be careful that your compiler doesn't actuallysupport these functions at all (this could happen if you include your owndeclaration of a function to get around a compile-time error). If yourcompiler should support the function, then fixing this problem usually requirescompiler-specific settings. You'll generally want to look for how to tell thecompiler where to look for libraries and make sure that the libraries were actually installed correctly.
Linker errors can also come about in functions that you have declaredand defined if you fail to include all of the necessary object files in thelinking process. For example, if you write your class definition inmyClass.cc, and your main function is in myMain.cc, your compiler will createtwo object files, myClass.o and myMain.o, and the linker will need both of themto finish the creation of the new program. If you leave out myClass.o, then itwill not have the class definition even if you correctly included myClass.h!
A sometimes subtle error is when the linker complains about there being morethan one definition for a class, function, or variable. This issue can come upin one of several ways: first, there might actually be two definitions of anobject--for instance, two global variables both declared as external variablesto be accessible outside of the source code file. This is a legitimate concernfor both functions and variables, and it definitely can happen. On theother hand, sometimes the problem is with the directives to the linker; on morethan one occasion, I've seen people include multiple copies of the same object file in the linking process. And bingo, you've got multiple definitions. A typical giveaway for this problem is that a whole host of functions have multiple definitions.
The last bizarre type of linker error is a complain about an'undefined reference to main'. This particular linker error differs from theother in that it may have nothing to do with including object files or havingthe correct paths to your libraries. Instead, it means that the linker triedto create an executable and couldn't figure out where the main() function waslocated. This can happen if you forget to include the main function at all, or if you attempt to compile code that was never meant to be a stand-alone executable (for instance, if you tried to compile a library).Related articles
Dev C++ Online
What's the difference between declaring and defining something in C and C++? Learn about the distinction between declaring a variable, class or function--and defining it--and why it matters when you have trouble compiling or linking your code
Learn more about dealing with compiler warnings Compiler warnings can indicate future bugs!
Dev C++ Does Not Show Errors
Compiling and Linking A brief description of the compiling and linking process
How To Check Error In Dev C++
The Static Keyword Covers the static keyword and how it can change the accessibility of global variablesUsing Namespaces Learn how namespaces can hide function and variable declarations
Advertising | Privacy policy |Copyright © 2019 Cprogramming.com | Contact | About
Comments are closed.