Errors List
From SA-MP Wiki
| Revision as of 09:51, 15 July 2012 Smithy (Talk | contribs) ← Previous diff |
Revision as of 16:30, 29 July 2012 Vince (Talk | contribs) (Error 047) Next diff → |
||
| Line 2: | Line 2: | ||
| This pages contains the most common errors and warnings produced by the pawn compiler when creating SA:MP scripts. | This pages contains the most common errors and warnings produced by the pawn compiler when creating SA:MP scripts. | ||
| - | When the compiler finds an error in a file, it outputs a message giving, in this order:<br/> | + | When the compiler finds an error in a file, it outputs a message giving, in this order: |
| - | • the name of the file<br/> | + | * the name of the file |
| - | • the line number were the compiler detected the error between parentheses, directly behind the filename<br/> | + | * the line number were the compiler detected the error between parentheses, directly behind the filename |
| - | • the error class (“error”, “fatal error” or “warning”)<br/> | + | * the error class (error, fatal error or warning) |
| - | • an error number<br/> | + | * an error number |
| - | • a descriptive error message<br/><br/> | + | * a descriptive error message |
| For example:<br/> | For example:<br/> | ||
| Line 31: | Line 31: | ||
| == Common Errors== | == Common Errors== | ||
| - | ===035: argument type mismatch (argument x)=== | ||
| - | An argument passed to a function is of the wrong 'type'. | ||
| - | For example, passing a string where you should be passing an integer. | ||
| - | |||
| - | Example: | ||
| - | error 035: argument type mismatch (argument 1) | ||
| - | <pawn> | ||
| - | Kick("playerid"); // We are passing a STRING, we should be passing an INTEGER | ||
| - | </pawn> | ||
| - | |||
| - | '''Fixed:''' | ||
| - | <pawn> | ||
| - | Kick(playerid); | ||
| - | </pawn> | ||
| - | |||
| ===001: expected token=== | ===001: expected token=== | ||
| A required token is missing. | A required token is missing. | ||
| Line 98: | Line 83: | ||
| </pawn> | </pawn> | ||
| + | ===035: argument type mismatch (argument x)=== | ||
| + | An argument passed to a function is of the wrong 'type'. | ||
| + | For example, passing a string where you should be passing an integer. | ||
| + | Example: | ||
| + | error 035: argument type mismatch (argument 1) | ||
| + | <pawn> | ||
| + | Kick("playerid"); // We are passing a STRING, we should be passing an INTEGER | ||
| + | </pawn> | ||
| + | '''Fixed:''' | ||
| + | <pawn> | ||
| + | Kick(playerid); | ||
| + | </pawn> | ||
| + | |||
| + | ===047: array sizes do not match, or destination array is too small=== | ||
| + | For array assignment, the arrays on the left and the right side of the | ||
| + | assignment operator must have the same number of dimensions. | ||
| + | In addition: | ||
| + | * for multi-dimensional arrays, both arrays must have the same size; | ||
| + | * for single arrays with a single dimension, the array on the left side of the assignment operator must have a size that is equal or bigger than the one on the right side. | ||
| + | |||
| + | <pawn> | ||
| + | new destination[8]; | ||
| + | new msg[] = "Hello World!"; | ||
| + | |||
| + | destination = msg; | ||
| + | </pawn> | ||
| + | |||
| + | In the above code, we try to fit 12 characters in an array that can only support 8. By increasing the array size of the destination, we can solve this. Note that a string also requires a null terminator so the total length of "Hello World!" plus the null terminator is, in fact, 13. | ||
| + | <pawn> | ||
| + | new destination[13]; | ||
| + | new msg[] = "Hello World!"; | ||
| + | |||
| + | destination = msg; | ||
| + | </pawn> | ||
| ==Common Warnings== | ==Common Warnings== | ||
Revision as of 16:30, 29 July 2012
Contents |
General Pawn Error List
This pages contains the most common errors and warnings produced by the pawn compiler when creating SA:MP scripts.
When the compiler finds an error in a file, it outputs a message giving, in this order:
- the name of the file
- the line number were the compiler detected the error between parentheses, directly behind the filename
- the error class (error, fatal error or warning)
- an error number
- a descriptive error message
For example:
hello.pwn(3) : error 001: expected token: ";", but found "{"
Note: The error may be on the line ABOVE the line that is shown, since the compiler cannot always establish an error before having analyzed the complete expression.
Error categories
Errors are separated into three classes:
Errors
- Describe situations where the compiler is unable to generate appropriate code.
- Errors messages are numbered from 1 to 99.
Fatal errors
- Fatal errors describe errors from which the compiler cannot recover.
- Parsing is aborted.
- Fatal error messages are numbered from 100 to 199.
Warnings
- Warnings are displayed for unintended compiler assumptions and common mistakes.
- Warning messages are numbered from 200 to 299.
Common Errors
001: expected token
A required token is missing.
Example:
error 001: expected token: ";", but found "return"
main() { print("test") // This line is missing a semi-colon. That is the token it is expecting. return 1; // The error states that it found "return", this is this line it is referring to, // as it is after the point at which the missing token (in this case the semi-colon) should be. }
002: only a single statement (or expression) can follow each “case”
Every case in a switch statement can hold exactly one statement.
To put multiple statements in a case, enclose these statements
between braces (which creates a compound statement).
Example:
error 002: only a single statement (or expression) can follow each "case"
main() { switch(x) { case 0: print("hello"); print("hello"); } return 1; }
The above code also produces other warnings/errors:
error 002: only a single statement (or expression) can follow each "case" warning 215: expression has no effect error 010: invalid function or declaration
Fixed:
main() { switch(x) { case 0: { print("hello"); print("hello"); } } return 1; }
035: argument type mismatch (argument x)
An argument passed to a function is of the wrong 'type'. For example, passing a string where you should be passing an integer.
Example:
error 035: argument type mismatch (argument 1)
Kick("playerid"); // We are passing a STRING, we should be passing an INTEGER
Fixed:
Kick(playerid);
047: array sizes do not match, or destination array is too small
For array assignment, the arrays on the left and the right side of the assignment operator must have the same number of dimensions. In addition:
- for multi-dimensional arrays, both arrays must have the same size;
- for single arrays with a single dimension, the array on the left side of the assignment operator must have a size that is equal or bigger than the one on the right side.
new destination[8]; new msg[] = "Hello World!"; destination = msg;
In the above code, we try to fit 12 characters in an array that can only support 8. By increasing the array size of the destination, we can solve this. Note that a string also requires a null terminator so the total length of "Hello World!" plus the null terminator is, in fact, 13.
new destination[13]; new msg[] = "Hello World!"; destination = msg;
Common Warnings
211: possibly unintended assignment
The assignment operator (=) was found in an if-statement, instead of the equality operator (==). If the assignment is intended, the expression must be wrapped in parentheses. Example:
if(a = 2) // warning if(a == 2) // no warning if((a = 2)) // no warning; the value 2 will be assigned to variable a and the expression will always return true.
217: loose indentation
The compiler will issue this warning if the code indentation is 'loose', example:
Good:
if(condition) { action(); result(); }
Bad:
if(condition) { action(); result(); }
Indentation means to push (indent) text along from the left of the page (by pressing the TAB key). This is common practise in programming to make code easier to read.
