Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to Debug your Code
DynamicSquid (4631)

I see this question a lot.

My code isn't working!

Well, in this tutorial, I'm going to fix that.

How to read error messages

Let's take a look at an error.

#include <iostream>

int main()
  cout << "Hello World!\n";

  // the correct way should be:
  // std::cout << "Hello World!\n";

Here's the error message:

Now this may seem confusing to read, but let's break it down.

main.cpp:5:3: error: use of undeclared identitifer 'cout'; did you mean 'std::cout'?

cout << "Hello World!\n";

Now the first part tells us where the error is located, and some information about it.

main.cpp is the file name where it's located in, and 5:3 tells us it's on line 5, and 3 characters in. You don't really need to worry about the 3 in this case. Just focus on which line it's on.

The next thing we know is that there's an error. What's the error? use of undeclared identitifer 'cout'. That means repl.it does not know what cout is!

And the cool thing is, it even suggests to us how to fix the problem! Isn't that neat?

Now the next part contains a bunch of jarble like /usr/bin/../lib/gcc and all that. Don't worry about that. That's for some more detailed information. For now, let's focus on the first part.

Let's take a look at another example.

int main()
  const int dontChangeMe = 10;
  dontChangeMe = 20;

// 'const' means that a variable cannot be changed

Here's the error message:

Again, let's break this down.

main.cpp:4:16: error: cannot assign to variable `dontChangeMe` with const-qualified type `const int`

dontChangeMe = 20;
~~~~~~~~~~~~ ^

As you can see our error is in the main.cpp file on line 4. The error is cannot assign to variable 'dontChangeMe' with const-qualified type 'const int' which basically means dontChangeMe cannot be changed. It even shows us where our error is!

Now it actually shows us a second error since, we declared a constant but tried to change it (line 3), and we tried to change a constant (line 4).

But those two errors basically mean the same thing.


There are 2 things to take away from this:

  1. Where it is
  2. What it is
where it is            what it is
file_name:line_number: error: error_message
main.cpp: 4:16:        error: cannot assign to variable 'dontChangeMe' with const-qualified type

And even if you're still confused about error messages, don't worry, embrace them! Spend some time looking at it, and try to understand it.

Once you learn about your error messages, you can solve all your bugs!

Unless you're doing Processing and not matter what the bug is, the error message always reads: error - you suck.

But even still, if you have a burning question that you can't solve, don't ask this:

My code isn't working!

Ask this:

What does this error message mean?