Skip to main content

First Program Β» Common Problems

It is worth paying attention to what can cause a compilation error. Compilation errors are caused by invalid C++. When the compiler encounters invalid code, it will stop and tell you what's wrong. You cannot run your program unless compilation is completed without errors.

Forgetting about std:: prefix​

An error is caused if you omit std:: without the appropriate using statement.

πŸ”΄ Error message​

error: β€˜cout’ was not declared in this scope; did you mean β€˜std::cout’?

πŸ–Ό Illustration of the error:​

Illustration of the error - omitting std

πŸ‘Œ Solution​

Add using std::cout; to the top of your main function:

#include <iostream>
int main() {
using std::cout;
cout << "Hello, World!";
}

Forgotten includes​

It's very easy to forget an #include, even for experts.

πŸ”΄ Error message​

error: β€˜cout’ is not a member of β€˜std’

πŸ–Ό Illustration of the error:​

Illustration of the error - missing include

Missing semicolons ;​

Beginners often forget the semicolons (;) at the end of statements. Study the code examples provided in this lesson carefully, paying attention to where they should go.

πŸ”΄ Error message​

error: expected `;` before (...)

πŸ–Ό Illustration of the error:​

Illustration of the error - missing semicolon
Flaky Error Messages

Compilers have to guess where you missed a semicolon. Therefore if you are sure that you didn't miss a semicolon on the line it tells you about, try looking up a few lines.

Additionally missing semicolons can also cause unrelated errors further down your code. Usually the first error message given by the compiler is the most helpful one.

Unterminated text​

Text is always between the quote characters "TEXT IS HERE!". Forgetting to add the second quotation mark leads to compiler thinking the rest of your program is text causing numerous errors.

πŸ”΄ Error message​

error: missing terminating " character

πŸ–Ό Illustration of the error:​

Illustration of the error - missing quote

Using " inside of text​

You may be tempted to put quotes inside a message when outputting it to the console. However, remember that C++ considers everything inside of "..." quotes to be text.

Thus, if you write:

"Hi! My name is "Slim Shady". Nice to meet you!"

C++ will interpret this as:

"TEXT" C++ CODE "TEXT"

πŸ”΄ Error message​

error: unable to find string literal operator β€˜operator""Slim’ with β€˜const char [16]’, β€˜long unsigned int’ arguments

πŸ–Ό Illustration of the error:​

TODO

Unhelpful errors

These error messages aren't that helpful! Your hint should be the fact that it is treating what you intend to be text as code.

πŸ‘Œ Solution​

You can prevent this by using the \ backslash character to escape the quote.

"Hi! My name is \"Slim Shady\". Nice to meet you!"
Escape sequence

The \" is an example of an escape sequence. It tells the compiler to treat the next character as text.

We can also use the backslash to escape other characters, such as the backslash itself. As a result, we'll need to use two backslashes to get one in the output.

Code
std::cout << "C:\\Users\\SlimShady\\Desktop";
Console
C:\Users\SlimShady\Desktop

First Program Β» Common Problems

It is worth paying attention to what can cause a compilation error. Compilation errors are caused by invalid C++. When the compiler encounters invalid code, it will stop and tell you what's wrong. You cannot run your program unless compilation is completed without errors.

Forgetting about std:: prefix​

An error is caused if you omit std:: without the appropriate using statement.

πŸ”΄ Error message​

error: β€˜cout’ was not declared in this scope; did you mean β€˜std::cout’?

πŸ–Ό Illustration of the error:​

Illustration of the error - omitting std

πŸ‘Œ Solution​

Add using std::cout; to the top of your main function:

#include <iostream>
int main() {
using std::cout;
cout << "Hello, World!";
}

Forgotten includes​

It's very easy to forget an #include, even for experts.

πŸ”΄ Error message​

error: β€˜cout’ is not a member of β€˜std’

πŸ–Ό Illustration of the error:​

Illustration of the error - missing include

Missing semicolons ;​

Beginners often forget the semicolons (;) at the end of statements. Study the code examples provided in this lesson carefully, paying attention to where they should go.

πŸ”΄ Error message​

error: expected `;` before (...)

πŸ–Ό Illustration of the error:​

Illustration of the error - missing semicolon
Flaky Error Messages

Compilers have to guess where you missed a semicolon. Therefore if you are sure that you didn't miss a semicolon on the line it tells you about, try looking up a few lines.

Additionally missing semicolons can also cause unrelated errors further down your code. Usually the first error message given by the compiler is the most helpful one.

Unterminated text​

Text is always between the quote characters "TEXT IS HERE!". Forgetting to add the second quotation mark leads to compiler thinking the rest of your program is text causing numerous errors.

πŸ”΄ Error message​

error: missing terminating " character

πŸ–Ό Illustration of the error:​

Illustration of the error - missing quote

Using " inside of text​

You may be tempted to put quotes inside a message when outputting it to the console. However, remember that C++ considers everything inside of "..." quotes to be text.

Thus, if you write:

"Hi! My name is "Slim Shady". Nice to meet you!"

C++ will interpret this as:

"TEXT" C++ CODE "TEXT"

πŸ”΄ Error message​

error: unable to find string literal operator β€˜operator""Slim’ with β€˜const char [16]’, β€˜long unsigned int’ arguments

πŸ–Ό Illustration of the error:​

TODO

Unhelpful errors

These error messages aren't that helpful! Your hint should be the fact that it is treating what you intend to be text as code.

πŸ‘Œ Solution​

You can prevent this by using the \ backslash character to escape the quote.

"Hi! My name is \"Slim Shady\". Nice to meet you!"
Escape sequence

The \" is an example of an escape sequence. It tells the compiler to treat the next character as text.

We can also use the backslash to escape other characters, such as the backslash itself. As a result, we'll need to use two backslashes to get one in the output.

Code
std::cout << "C:\\Users\\SlimShady\\Desktop";
Console
C:\Users\SlimShady\Desktop