samedi 16 octobre 2021

compiler gives uninitialized local variable error despite initialized variable

I searched for this question, but couldn't find anything. If asked earlier, please inform me (share link, I'll modify the question).

I used to get the Declarations in Conditions topics in C++ and faced the below problem.

#include <iostream>
using namespace std;
int main() {
  if(int a = 4 && a != 3) {
    cout << "Bug fixed!" << endl;
  }
}

I declared and then initialized the variable a. In the The C++ Programming Language by Bjarne Stroustrup Ed.2011, it is said:

The scope of variable declared in if statement extends from its point of declaration to the end of the statement that the condition controls.

That's, I declared and initialized the variable a, but when I try to compare it against 3, compiler gives uninitialized local variable a used error. Why, what's the problem?

Aucun commentaire:

Enregistrer un commentaire