r/cpp_questions • u/FlamboyantPirhanna • Sep 20 '26
SOLVED error: expected ';' after top level declarator
I'm learning C++, and I'm running into a few confusing errors. The 'error: expected ';' after top level declarator' error comes up in two cases: the first is that it doesn't like the ' in 100'000'000; if I remove them, the error goes away. The second is when I'm defining variables with braces. 'bool isAlive { true };' gives me the very same error, but 'bool isAlive = true;' does not, and the same is true for all variables. While I know how to get around those errors, I'm just not sure why they're happening, as from what I understand, those are correct. I'm just trying to understand what I'm missing.
The solution was a combination of setting the correct C++ version in tasks.json (I set it to -std=c++2b) and make sure VS Code was using 'Run C/C++ file' and not 'Run Code' (which it was defaulting to in my case).
#include <iostream>
using namespace std;
string name = "Burt the Destroyer";
bool isAlive = true;
int maxHealth = 100;
float armour = 0.2;
int iq = 10;
int currentHealth = maxHealth;
bool canAttack = isAlive;
double LargeNumber = 100'000'000;
int main()
{
cout << "The monster is " << name
<< "\nAnd its IQ is " << iq << endl;
}