In this video, we'll talk about compiler errors and why they can happen.
Programming languages have very specific rules about how to write code,
so that the compiler can translate that code to machine code.
These rules involve both structure and meaning.
For example, c++ says that all variable declarations
must end with a semicolon.
If we don't add the semicolon at the end or we put it in the middle instead,
then we have a syntax error.
The compiler is complaining that the structure is not correct.
In the first example, we didn't terminate the string error
with a trailing quote character.
In the second example, we left out the semicolon at the end of the return 0 statement.
Both of these are syntax errors. There's something wrong with the structure of the code.
Now these syntax errors are pretty easy to fix, and the compiler errors
usually give us pretty good information about what's going on.
Semantic errors are a little different.
In this case, the syntax or structure is correct.
But the meaning of the code doesn't make sense to the compiler.
Suppose we want to add two variables, a and b.
If the variables are both integers, then this makes perfect sense.
But what happens if one of the variables
is a person and the other variable is an integer.
Does it make sense to add a person and an integer?
Probably not. In this case, the compiler generates an error.
When we think of compiler errors, we really think about both of these classes of errors.
Remember, compilers are very, very picky. Why,
because their job is to translate your code that you wrote into machine
code that functions as you said.
If the compiler has any problem understanding the meaning of your code,
then it won't guess, it will generate an error.
Let's head over to CodeLite and see some of these errors.
So now I'm in CodeLite and I created a project called compiler errors.
And I created it right from our template, so it's just a simple
hello world program which is printing out hello world.
Now what we want to do is we want to introduce some compiler errors into this program,
so you can see what they're like. Now let's do some --
first let's do some syntax errors.
Simple stuff like okay we forgot this quote right here.
And I can save and compile,
and you'll see that I'll get an error that says
missing terminating character and if you click on here it takes you here.
Okay. Now in this case we've got another error
that says expected primary expression before return.
This happens sometimes.
The compiler gets confused it sees that first error and.
Then it's trying to figure out what's going on and it goes to the next line
and it says i don't know where I'm at it creates an error.
Many times when you fix that first error, many of the subsequent errors go away.
So let's put that quote back in.
We'll save, and let's recompile.
Now we've got a clean compile.
So that would be my advice. When you're first learning,
start out at the beginning, get that first syntax error fixed, recompile
and very likely a whole bunch of other errors just went away.
Okay. So that's an example of a syntax error.The compiler is expecting
a string and it knows it's got a rule that says all strings have to have a quote character,
a bunch of characters and then a quote character at the end to terminate it.
It didn't see that. It didn't like that. And there's the syntax error, really easy to fix.
Other times you may have things that are misspelled.
So for example, let's say I spelt end
l here wrong, I put an extra l in there. Now when i compile,
it's saying error. And ll
is not a member of the standard namespace.
So it knows that cout is and endl is
but endll is not.
So in this case,
this error message kind of leads you to the place
where the problem is. And just oh of course I have a typo. So you can fix that as well.
Okay. Another example of a compiler error
that's a syntax error is forgetting the semicolon here let's say
at the return 0.
All statements have to end with a semicolon, so we just forgot it
that violates one of the rules that the compiler
knows about and it doesn't like it. So what we'll do here again we'll compile
and you can see it says error expecting a semicolon
before that curly.
So there's the curly, and it's expecting a semicolon
sure is because of that.
Okay. Now suppose we just say return semicolon.
Now this is problematic because we've told the compiler
in our code that main returns an integer.
But here, we're just returning. We're not returning an integer. So let's see what it says.
Let's compile,
and it says error return statement with no value
in function returning integer. Pretty good statement, right.
It's telling you I've got a function. You told me it's returning an integer, but you're not returning any
value now. So I can
return 0.
Okay. So let me show you an example of
a semantic error, something like this.
This is perfectly valid. I can return an integer. I can return a string.
I can return a lot of things. The problem is
it doesn't make sense to the compiler because you've told it here
that you're returning an integer but now you're returning a string.
So let's run that and see what you get.
Here you go. Error, invalid conversion from a character star, which is a string
to an integer. So the compiler is trying to figure out
how do I convert that Joe to an integer, I don't know.
I don't have -- I have no idea. I'll just throw the error.
Okay. And that's up to you now to fix it. So in this case, again, I'll fix it to return 0.
Now let's do another one. Let's get rid of that first curly.
This is a real interesting compiler error. And I'll compile it, and then we'll talk about it.
Let's compile, and you can see that I've got three errors from that.
What's happening is the compiler is totally lost here.
The compiler is seeing int main and then it's expecting a curly
and it's processing more and more stuff kind of hoping to see that
left curly eventually,
and it's not seeing it so it's seeing other things and it's throwing errors it's saying
I don't know what's going on before the std.
I've got an unexpected, unqualified something before return.
I'm lost. Okay.
So when you see these kinds of of errors, I mean this is a tiny little program,
and I've got three compiler errors.
Sometimes you'll compile, something you'll get 20, 40 errors.
And many times when you fix that first one, most of them will go away.
So in this case, all that's missing is that curly.
So I'll compile,
and we should have a clean compile now that we do.
Sometimes you'll add extra curlies at the end,
which again is illegal. So I'll compile
and there it is.
An error I'm expecting something before that token. It doesn't know what you're doing.
So we'll get rid of that.
Okay. So let's do one more semantic error. Let's say that we've got hello world in here.
And I'm going to put parentheses around it, and I'm going to divide it by
125. And I'll put a parenthesis around that, just simple math.
Okay, that's pretty weird.
I've got something here
divided by something here. That's valid. I can divide two things
the division operator which is that slash is a binary operator.
It works on two things, right. So 4 divided by 2 is 2.,
8 divided by 2 is 4 and so forth. But in this case,
the syntax is correct.
But the compiler is going to say I have no idea what you want me to do here.
You're telling me to divide hello world by 125,
I don't even know what that means. So let's compile that
and the error is something like error invalid operands.
So the division operator, the slash operator
has got invalid operands.
There's two operands, right.
One is a string, the other one is an integer. And it's saying I don't know what to do here.
So these are some simple examples of compiler errors.
Don't be afraid of the compiler errors. You're going to get them.
Don't let them frustrate you. If you have any that you can't figure out,
please post to the forums and I'll be glad to help you out.
It's just something that we have to learn to work with because we're going to get compiler errors.
We all make mistakes.
Sometimes we're prototyping. We're trying new things, and we're getting errors.
Sometimes they're just silly mistakes and they're hard to find sometimes.
Sometimes you'll be looking at your own code. I can tell you this,
this little error here with the the end double ll,
this is a good error message that you got. But if you're looking at this code
and you've been coding for a few hours,
I don't think you'll see those two ll, most likely they look like one l.
So this you know this is the kind of thing that programming is about.
One of the important things is getting rid of all these compiler errors.
Once we get rid of the compiler errors, then we can test our code,
and we can look for logic errors and so forth.
Okay. So that wraps up this video. In the next video,
we'll talk a little bit about compiler warnings.
They're a little bit different from errors because they're not true errors
but they're telling you something that you should look out for.