1 00:00:05,600 --> 00:00:09,500 In this video, we'll talk about compiler errors and why they can happen. 2 00:00:10,490 --> 00:00:14,480 Programming languages have very specific rules about how to write code, 3 00:00:14,480 --> 00:00:17,480 so that the compiler can translate that code to machine code. 4 00:00:18,380 --> 00:00:21,380 These rules involve both structure and meaning. 5 00:00:21,680 --> 00:00:25,180 For example, c++ says that all variable declarations 6 00:00:25,180 --> 00:00:26,740 must end with a semicolon. 7 00:00:27,740 --> 00:00:31,640 If we don't add the semicolon at the end or we put it in the middle instead, 8 00:00:31,640 --> 00:00:33,240 then we have a syntax error. 9 00:00:33,240 --> 00:00:36,840 The compiler is complaining that the structure is not correct. 10 00:00:38,340 --> 00:00:41,540 In the first example, we didn't terminate the string error 11 00:00:41,540 --> 00:00:43,540 with a trailing quote character. 12 00:00:43,540 --> 00:00:48,140 In the second example, we left out the semicolon at the end of the return 0 statement. 13 00:00:48,840 --> 00:00:53,540 Both of these are syntax errors. There's something wrong with the structure of the code. 14 00:00:54,040 --> 00:00:57,340 Now these syntax errors are pretty easy to fix, and the compiler errors 15 00:00:57,340 --> 00:01:00,340 usually give us pretty good information about what's going on. 16 00:01:01,140 --> 00:01:02,940 Semantic errors are a little different. 17 00:01:02,940 --> 00:01:05,940 In this case, the syntax or structure is correct. 18 00:01:05,940 --> 00:01:08,940 But the meaning of the code doesn't make sense to the compiler. 19 00:01:10,040 --> 00:01:12,500 Suppose we want to add two variables, a and b. 20 00:01:13,380 --> 00:01:16,880 If the variables are both integers, then this makes perfect sense. 21 00:01:17,180 --> 00:01:19,180 But what happens if one of the variables 22 00:01:19,180 --> 00:01:22,080 is a person and the other variable is an integer. 23 00:01:22,080 --> 00:01:24,680 Does it make sense to add a person and an integer? 24 00:01:24,980 --> 00:01:28,880 Probably not. In this case, the compiler generates an error. 25 00:01:29,680 --> 00:01:34,180 When we think of compiler errors, we really think about both of these classes of errors. 26 00:01:34,680 --> 00:01:38,080 Remember, compilers are very, very picky. Why, 27 00:01:38,080 --> 00:01:42,680 because their job is to translate your code that you wrote into machine 28 00:01:42,680 --> 00:01:44,280 code that functions as you said. 29 00:01:45,080 --> 00:01:48,580 If the compiler has any problem understanding the meaning of your code, 30 00:01:48,580 --> 00:01:51,380 then it won't guess, it will generate an error. 31 00:01:52,380 --> 00:01:54,680 Let's head over to CodeLite and see some of these errors. 32 00:01:56,580 --> 00:02:00,180 So now I'm in CodeLite and I created a project called compiler errors. 33 00:02:01,080 --> 00:02:04,380 And I created it right from our template, so it's just a simple 34 00:02:04,380 --> 00:02:06,880 hello world program which is printing out hello world. 35 00:02:07,480 --> 00:02:11,470 Now what we want to do is we want to introduce some compiler errors into this program, 36 00:02:11,470 --> 00:02:14,070 so you can see what they're like. Now let's do some -- 37 00:02:14,070 --> 00:02:16,570 first let's do some syntax errors. 38 00:02:16,570 --> 00:02:20,170 Simple stuff like okay we forgot this quote right here. 39 00:02:20,670 --> 00:02:22,670 And I can save and compile, 40 00:02:23,330 --> 00:02:25,530 and you'll see that I'll get an error that says 41 00:02:25,530 --> 00:02:28,830 missing terminating character and if you click on here it takes you here. 42 00:02:29,530 --> 00:02:32,030 Okay. Now in this case we've got another error 43 00:02:32,030 --> 00:02:35,330 that says expected primary expression before return. 44 00:02:35,930 --> 00:02:37,430 This happens sometimes. 45 00:02:37,430 --> 00:02:40,190 The compiler gets confused it sees that first error and. 46 00:02:40,190 --> 00:02:42,890 Then it's trying to figure out what's going on and it goes to the next line 47 00:02:42,890 --> 00:02:45,090 and it says i don't know where I'm at it creates an error. 48 00:02:45,090 --> 00:02:48,890 Many times when you fix that first error, many of the subsequent errors go away. 49 00:02:48,890 --> 00:02:50,790 So let's put that quote back in. 50 00:02:51,690 --> 00:02:53,690 We'll save, and let's recompile. 51 00:02:55,390 --> 00:02:57,190 Now we've got a clean compile. 52 00:02:57,190 --> 00:03:00,390 So that would be my advice. When you're first learning, 53 00:03:00,390 --> 00:03:04,790 start out at the beginning, get that first syntax error fixed, recompile 54 00:03:04,790 --> 00:03:08,090 and very likely a whole bunch of other errors just went away. 55 00:03:08,090 --> 00:03:12,690 Okay. So that's an example of a syntax error.The compiler is expecting 56 00:03:13,390 --> 00:03:17,990 a string and it knows it's got a rule that says all strings have to have a quote character, 57 00:03:18,290 --> 00:03:21,590 a bunch of characters and then a quote character at the end to terminate it. 58 00:03:21,590 --> 00:03:25,890 It didn't see that. It didn't like that. And there's the syntax error, really easy to fix. 59 00:03:26,590 --> 00:03:28,890 Other times you may have things that are misspelled. 60 00:03:28,890 --> 00:03:31,390 So for example, let's say I spelt end 61 00:03:31,390 --> 00:03:35,380 l here wrong, I put an extra l in there. Now when i compile, 62 00:03:37,740 --> 00:03:40,640 it's saying error. And ll 63 00:03:40,640 --> 00:03:43,640 is not a member of the standard namespace. 64 00:03:43,640 --> 00:03:47,200 So it knows that cout is and endl is 65 00:03:47,200 --> 00:03:49,000 but endll is not. 66 00:03:49,000 --> 00:03:50,000 So in this case, 67 00:03:50,500 --> 00:03:52,860 this error message kind of leads you to the place 68 00:03:52,860 --> 00:03:57,630 where the problem is. And just oh of course I have a typo. So you can fix that as well. 69 00:03:58,430 --> 00:04:01,130 Okay. Another example of a compiler error 70 00:04:01,130 --> 00:04:04,830 that's a syntax error is forgetting the semicolon here let's say 71 00:04:04,830 --> 00:04:06,330 at the return 0. 72 00:04:06,330 --> 00:04:09,690 All statements have to end with a semicolon, so we just forgot it 73 00:04:09,690 --> 00:04:12,190 that violates one of the rules that the compiler 74 00:04:12,190 --> 00:04:15,990 knows about and it doesn't like it. So what we'll do here again we'll compile 75 00:04:17,649 --> 00:04:21,350 and you can see it says error expecting a semicolon 76 00:04:21,350 --> 00:04:22,850 before that curly. 77 00:04:23,650 --> 00:04:26,950 So there's the curly, and it's expecting a semicolon 78 00:04:26,950 --> 00:04:28,250 sure is because of that. 79 00:04:28,850 --> 00:04:32,450 Okay. Now suppose we just say return semicolon. 80 00:04:33,110 --> 00:04:35,810 Now this is problematic because we've told the compiler 81 00:04:35,810 --> 00:04:38,110 in our code that main returns an integer. 82 00:04:38,810 --> 00:04:42,110 But here, we're just returning. We're not returning an integer. So let's see what it says. 83 00:04:42,610 --> 00:04:43,610 Let's compile, 84 00:04:44,710 --> 00:04:47,910 and it says error return statement with no value 85 00:04:47,910 --> 00:04:50,810 in function returning integer. Pretty good statement, right. 86 00:04:50,810 --> 00:04:54,810 It's telling you I've got a function. You told me it's returning an integer, but you're not returning any 87 00:04:54,810 --> 00:04:56,810 value now. So I can 88 00:04:57,810 --> 00:04:58,710 return 0. 89 00:04:59,910 --> 00:05:02,210 Okay. So let me show you an example of 90 00:05:03,210 --> 00:05:05,810 a semantic error, something like this. 91 00:05:07,510 --> 00:05:10,810 This is perfectly valid. I can return an integer. I can return a string. 92 00:05:10,810 --> 00:05:13,810 I can return a lot of things. The problem is 93 00:05:13,810 --> 00:05:17,010 it doesn't make sense to the compiler because you've told it here 94 00:05:17,010 --> 00:05:20,010 that you're returning an integer but now you're returning a string. 95 00:05:20,510 --> 00:05:22,110 So let's run that and see what you get. 96 00:05:24,610 --> 00:05:29,110 Here you go. Error, invalid conversion from a character star, which is a string 97 00:05:29,410 --> 00:05:31,810 to an integer. So the compiler is trying to figure out 98 00:05:31,810 --> 00:05:35,010 how do I convert that Joe to an integer, I don't know. 99 00:05:35,010 --> 00:05:37,010 I don't have -- I have no idea. I'll just throw the error. 100 00:05:38,010 --> 00:05:42,210 Okay. And that's up to you now to fix it. So in this case, again, I'll fix it to return 0. 101 00:05:43,710 --> 00:05:46,710 Now let's do another one. Let's get rid of that first curly. 102 00:05:47,210 --> 00:05:51,210 This is a real interesting compiler error. And I'll compile it, and then we'll talk about it. 103 00:05:52,010 --> 00:05:56,210 Let's compile, and you can see that I've got three errors from that. 104 00:05:56,650 --> 00:05:59,650 What's happening is the compiler is totally lost here. 105 00:06:00,050 --> 00:06:03,650 The compiler is seeing int main and then it's expecting a curly 106 00:06:03,950 --> 00:06:07,550 and it's processing more and more stuff kind of hoping to see that 107 00:06:07,550 --> 00:06:08,850 left curly eventually, 108 00:06:09,050 --> 00:06:12,850 and it's not seeing it so it's seeing other things and it's throwing errors it's saying 109 00:06:12,850 --> 00:06:15,850 I don't know what's going on before the std. 110 00:06:16,350 --> 00:06:20,010 I've got an unexpected, unqualified something before return. 111 00:06:20,910 --> 00:06:22,510 I'm lost. Okay. 112 00:06:22,510 --> 00:06:25,910 So when you see these kinds of of errors, I mean this is a tiny little program, 113 00:06:25,910 --> 00:06:27,510 and I've got three compiler errors. 114 00:06:27,510 --> 00:06:31,010 Sometimes you'll compile, something you'll get 20, 40 errors. 115 00:06:31,010 --> 00:06:34,710 And many times when you fix that first one, most of them will go away. 116 00:06:34,710 --> 00:06:38,010 So in this case, all that's missing is that curly. 117 00:06:39,610 --> 00:06:40,600 So I'll compile, 118 00:06:41,200 --> 00:06:43,200 and we should have a clean compile now that we do. 119 00:06:44,000 --> 00:06:46,200 Sometimes you'll add extra curlies at the end, 120 00:06:47,200 --> 00:06:50,200 which again is illegal. So I'll compile 121 00:06:52,900 --> 00:06:54,000 and there it is. 122 00:06:54,000 --> 00:06:58,500 An error I'm expecting something before that token. It doesn't know what you're doing. 123 00:06:59,000 --> 00:07:00,200 So we'll get rid of that. 124 00:07:02,800 --> 00:07:06,800 Okay. So let's do one more semantic error. Let's say that we've got hello world in here. 125 00:07:07,200 --> 00:07:10,700 And I'm going to put parentheses around it, and I'm going to divide it by 126 00:07:10,700 --> 00:07:14,100 125. And I'll put a parenthesis around that, just simple math. 127 00:07:15,100 --> 00:07:17,600 Okay, that's pretty weird. 128 00:07:18,100 --> 00:07:19,800 I've got something here 129 00:07:20,400 --> 00:07:24,400 divided by something here. That's valid. I can divide two things 130 00:07:24,400 --> 00:07:28,400 the division operator which is that slash is a binary operator. 131 00:07:28,400 --> 00:07:31,700 It works on two things, right. So 4 divided by 2 is 2., 132 00:07:31,700 --> 00:07:35,300 8 divided by 2 is 4 and so forth. But in this case, 133 00:07:35,600 --> 00:07:36,800 the syntax is correct. 134 00:07:36,800 --> 00:07:40,300 But the compiler is going to say I have no idea what you want me to do here. 135 00:07:40,300 --> 00:07:43,180 You're telling me to divide hello world by 125, 136 00:07:43,180 --> 00:07:46,180 I don't even know what that means. So let's compile that 137 00:07:47,980 --> 00:07:51,480 and the error is something like error invalid operands. 138 00:07:52,080 --> 00:07:55,480 So the division operator, the slash operator 139 00:07:55,480 --> 00:07:56,980 has got invalid operands. 140 00:07:56,980 --> 00:07:58,280 There's two operands, right. 141 00:07:58,280 --> 00:08:01,940 One is a string, the other one is an integer. And it's saying I don't know what to do here. 142 00:08:02,640 --> 00:08:05,340 So these are some simple examples of compiler errors. 143 00:08:06,440 --> 00:08:09,440 Don't be afraid of the compiler errors. You're going to get them. 144 00:08:09,440 --> 00:08:12,440 Don't let them frustrate you. If you have any that you can't figure out, 145 00:08:12,440 --> 00:08:14,840 please post to the forums and I'll be glad to help you out. 146 00:08:15,440 --> 00:08:19,940 It's just something that we have to learn to work with because we're going to get compiler errors. 147 00:08:19,940 --> 00:08:21,440 We all make mistakes. 148 00:08:21,440 --> 00:08:24,800 Sometimes we're prototyping. We're trying new things, and we're getting errors. 149 00:08:24,800 --> 00:08:27,800 Sometimes they're just silly mistakes and they're hard to find sometimes. 150 00:08:27,800 --> 00:08:30,700 Sometimes you'll be looking at your own code. I can tell you this, 151 00:08:30,700 --> 00:08:34,580 this little error here with the the end double ll, 152 00:08:34,580 --> 00:08:37,780 this is a good error message that you got. But if you're looking at this code 153 00:08:37,780 --> 00:08:39,280 and you've been coding for a few hours, 154 00:08:39,780 --> 00:08:43,770 I don't think you'll see those two ll, most likely they look like one l. 155 00:08:43,770 --> 00:08:47,170 So this you know this is the kind of thing that programming is about. 156 00:08:47,170 --> 00:08:51,570 One of the important things is getting rid of all these compiler errors. 157 00:08:51,570 --> 00:08:54,570 Once we get rid of the compiler errors, then we can test our code, 158 00:08:54,570 --> 00:08:56,670 and we can look for logic errors and so forth. 159 00:08:56,670 --> 00:08:59,570 Okay. So that wraps up this video. In the next video, 160 00:08:59,570 --> 00:09:01,470 we'll talk a little bit about compiler warnings. 161 00:09:01,470 --> 00:09:04,770 They're a little bit different from errors because they're not true errors 162 00:09:04,770 --> 00:09:07,070 but they're telling you something that you should look out for.