[FIXED] Code::Blocks error: 'cout' was not declared in this scope

Code::Blocks error: 'cout' was not declared in this scope
Code::Blocks error: 'cout' was not declared in this scope
This is one of the most common errors you get if you download or create a C++ project on your Code::Blocks IDE and try to compile it using GNU GCC compiler (the default compiler in Code::Blocks). This problem is common with people who've recently moved to Code::Blocks from an older IDE. This happens because Code::Blocks fails to see 'cout' keyword as a local scope unless its defined as a standard directory function.

Luckily, there are two simple ways to fix this error:

Method 1

Use this statement before your main function:

using namespace std;
So that your code looks something like:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!";
    return 0;
}

Method 2

Instead of using cout , try using std::cout so that the code looks something like:

#include <iostream>

int main()
{
    std::cout << "Hello world!";
    return 0;
}

Either way, you're telling the compiler to search for cout function in the standard directory (not current directory).
Note: If you're using Method 2, then you'll need to add that extra std:: to all your <iostream> functions, which is quite a hassle, tbh. I recommend using Method 1.

That's it! Now try to compile and run the program (assuming the program is written correctly), it should work.


Popular posts from this blog

How to know if someone has read your message with Read Receipt disabled on WhatsApp?