CodeWarrior C++ Syntax Warnings and Errors
EECS/CS 280
By Greg Lewis
This document is intended to help you fix common compile-time
warnings and errors that you may get while using CodeWarrior to
compile your projects. These errors and warnings are the way that
the compiler communicates to you where and what the syntax errors are
in your program, and can be very helpful if you know what they mean.
These warning and error messages are generated when you choose the
"Check Syntax" option under the "Project" menu of CodeWarrior.
The compiler issues a warning message when it thinks that you have
code that is suspicious, but it might also be what you intended to
do. Generally warnings indicate that something isn't correct in your
program, and when you hand in your program it should not have any
warnings in it. Warnings will allow your program to compile and run.
Error messages, on the other hand, mean that a serious error has
occurred and the compiler cannot figure out how to create an
executable program. Your program will not run until you fix all of
the error messages, and you will lose a substantial number of points
on your project if it has error messages when you submit it.
Errors and warnings have the following form in the message window in
CodeWarrior:
Warning : variable 'i' is not initialized before being used
polygon.cpp line 5 int i;
The first line tells whether the message is a warning or an error,
and gives a one-line description of what the problem may be. This
one-line description may look cryptic; a number of common
descriptions are explained below. Warning messages will have a
yellow construction sign with an exclamation mark in it next to the
message, and error messages will have a red stop sign.
The second line of the message gives important information about
where the error occurred. The name of the file is given (polygon.cpp
in this case), along with the line number and location of the error.
The part of the line that the compiler is complaining about is
underlined, such as the variable i above. This information is very
valuable for finding the cause of the error. If the underlined
portion itself is correct, then most likely you forgot something
directly before it that the compiler expects to see.
The best strategy for getting rid of compile-time errors is to fix
the first error on the list, and then choose "Check Syntax" after you
have fixed the first one to create an updated list of errors.
Earlier errors on the list can cause many "fake" errors to appear
later in your program because the compiler gets confused and can't
figure out what you are trying to do. If you try to fix the later
errors first, you may spend lots of time trying to figure out what is
wrong with a perfectly fine piece of source code when the true
problem is earlier in your program. When looking for the cause of an
error, be sure to check several lines above the line number given.
Frequently the compiler will not notice that an error has occurred
until several lines after the error.
This is not a complete list of messages, as there are hundreds of
different warning and error messages that you can receive. The
suggested solution given for each problem is only a general guess
about what the problem could be. Occasionally CodeWarrior will give
what is apparently one type of error but you really need to fix
something else to solve the problem. In the end there is no
substitute for looking carefully at the program to figure out what
exactly is causing the problem.
Error : exception handling option is disabled
Under the Edit menu, choose "PPC Std C++ Console Settings", then
click on "C/C++ Language." Then click on the box next to "Enable C++
Exceptions."
Error : undefined identifier 'ceil'
This error indicates that the compiler doesn't know what you are
referring to with the word in quotes ('ceil' in this case). This
could be for a variety for reasons. If the given word is a function,
you may not have included a proper header file ('math.h' in this
case). If it is a variable, then you probably did not declare that
variable. If all else fails, check your spelling.
Error : ';' expected
Error : ')' expected
Error : '{' expected
These errors are all related, in that you've forgotten to put in the
proper punctuation or match up your parentheses/brackets properly.
The indicated punctuation is missing someplace on the current line or
the immediately previous line of code. Add it and the problem should
disappear.
Error : not an lvalue
An lvalue is a variable name on the left side of the equal sign in an
assignment statement. Somewhere, probably on the line number given
in the error, you do not have a valid variable name on the left side
of an assignment operation. For example, this would occur if you have
ceil(sum) = sumsquared;
when it should really be
sum = ceil(sumsquared);
Error : illegal string constant
Somewhere you forgot to put a quotation mark at the end of a string.
Check the line number given in the error message (or lines before
that) and look for strings with no quotation mark at the end. This
error could also occur with strings that aren't valid in some other
way.
Error : function call does not match prototype
Check the function call on the given line. You probably have too
many or too few parameters to the function call, compared with the
prototype for the function given before main. Make the two parameter
lists match up both in number of parameters and data types of the
parameters and the error should go away.
Error : illegal function overloading
In this case, the compiler thinks you are trying to use an advanced
property of functions (overloading), and doing it incorrectly, when
you aren't even trying to use it. This is caused by a difference
between a function prototype and its corresponding function
definition. Check both and verify that they have exactly the same
data types for parameters and return values.
Error : cannot convert 'char *' to 'int'
Conversion errors generally occur in function calls, when you try to
pass the wrong type of variable as a parameter. Trying to pass a
string to a function, when the function definition defines an integer
in that position, is one example. Look at the definition of the
function and compare the actual parameters with the formal
parameters. This error can also occur in a simple assignment
statement which mixes incompatible data types, such as this one:
int i = "Hello";
Error : not a struct/union/class
You have probably tried to use a variable like a struct when it
really isn't one. Check the name given to the left of the period in
the error message, and change it to a valid struct variable. If you
are using an array of structs, you may have forgotten to put in an
array subscript, like this:
x.y = 4;
rather than
x[2].y = 4;
Error : 'y' is not a struct/union/class member
This is similar to the above error. The highlighted name ('y', in
this case) is used in your program as a member of a struct, but
according to the definition of that struct there is no such member.
Check to make sure that you have the correct type of struct, or that
you are trying to use the correct member variable.
Error : expression syntax error
Error : declaration syntax error
These are the two errors that CodeWarrior will give if it just can't
figure out what you are trying to say. They are the most vague
errors, and thus the hardest to give exact advice on how to fix.
Look carefully at the structure of your if, while, for, etc.
statements to make sure that they are written correctly. For
instance, this error will appear if you have an 'else' with no 'if'
before it that it can be matched with. Or, you will get it if you
have a semi-colon after your function definition. There are many
errors that could cause these error messages, so check everything
carefully.
Warning : variable 'j' is not initialized before being used
The compiler doesn't think that the variable given in quotes has been
initialized before trying to use it (printing it out, using it in an
equation, etc.). Usually you should always put some value in a
variable before using it, and to get rid of this warning you can
initialize it to 0 or something similar when you declare the variable.
Warning : return value expected
You declared a function to be a value-returning function (this
includes 'int main()'), and did not return anything before the
function exits. Look at what the function is supposed to do and
decide whether it needs to return any data. If not, put the word
'void' in front of the function prototype and definition as its
return type. If the function should return something, use the
'return' reserved word to do so, like this:
return StdDeviation;
Warning : function has no prototype
A function was defined that does not have a prototype. The line
number in the warning message should be the first line of the guilty
function. Copy this line to the function prototype before main
section (and add a semi-colon after it) to fix the error. Usually
this warning pops up after you have added a new function to your
program, but have forgotten to add a prototype for it. If you
already have this function in your prototype section, check the data
type of each parameter carefully; the prototype and function
definition probably differ in at least one of the parameters.