I am trying to initialize a 2 dimensional bool array to false with a constructor. I have a print function that is to display an '*' for each element of the array if each element is false, but instead it is printing the else statement as true.
Line 14: You're initializing a local array, not your member array. array goes out of scope at line 15. get rid of the word bool. You member array is never initialized and therefore contains garbage.
BTW, I have to guess here that array is a member variable of your class since you did not show you class declaration.,
Show the definition of the class. We cannot see how you declare the boolMatrix::array.
You have same issue as in this program:
1 2 3 4 5 6 7 8 9
#include <iostream>
int main() {
int answer = 7;
if (answer) {
int answer = 42;
}
std::cout << answer;
}
In function 'int main()':
6:13: warning: unused variable 'answer' [-Wunused-variable]
The answer is created and initialized but never used.
The otheranswer is used.
Do you understand why the answer of local scope masks/hides the answer of outer scope?