I declared a Boolean global variable in the ".h" file, and initialize it the ".cpp" file, I faced an error, i searched on the solution and found that i must define it as extern as following:
//in .h file
extern bool blindFound;
// in .cpp file
bool blindFound = false;
But when i print its value inside other methods, it gave me ( Null ) not false!!
Thanks,
printf(" blindFound: %s \n", blindFound );
Is it C or C++ ? Also your compiler should have warned you. Turn the warning on and pay attention to them.
Now the error is that you are printing a boolean as a string "%s". You should print it as a integer "%d". Then false will appear as 0 and true as 1.
You should use std::boolapha
is C++.
The answer Above is a little mistake.
http://www.parashift.com/c++-faq-lite/iostream-vs-stdio.html
Please try to avoid old printf
from C.
http://www.cplusplus.com/reference/ios/boolalpha/
// modify boolalpha flag
#include <iostream> // std::cout, std::boolalpha, std::noboolalpha
int main () {
bool b = true;
std::cout << std::boolalpha << b << '\n';
std::cout << std::noboolalpha << b << '\n';
return 0;
}
It is often better to wrap such variables in functions, making them local static objects. This effectively avoids order-of-initialization issues.
bool &blindFound()
{
static bool blindFound = false;
return blindFound;
}
Usage:
blindFound() = true; // set value
bool b = blindFound(); // read value
Have a look at the C++ FAQ for more information about the so-called "static initialization order fiasco".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.