C++: How to round a double to an int?

add 0.5 before casting (if x > 0) or subtract 0.5 (if x < 0), because the compiler will always truncate. C++11 also introduces std::round, which likely uses a similar logic of adding 0.5 to |x| under the hood (see the link if interested) but is obviously more robust. A follow up question might be why the float … Read more

libpng warning: iCCP: known incorrect sRGB profile

Libpng-1.6 is more stringent about checking ICC profiles than previous versions. You can ignore the warning. To get rid of it, remove the iCCP chunk from the PNG image. Some applications treat warnings as errors; if you are using such an application you do have to remove the chunk. You can do that with any … Read more

Compiling C++11 with g++

Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable. Assuming you are invoking g++ from the command line (terminal): $ g++ -std=c++11 your_file.cpp -o your_program or $ g++ -std=c++0x your_file.cpp -o your_program if the above doesn’t work.

What is the printf format specifier for bool?

There is no format specifier for bool types. However, since any integral type shorter than int is promoted to int when passed down to printf()‘s variadic arguments, you can use %d: But why not: or, better: or, even better: instead?

Why does the C++ STL not provide any “tree” containers?

There are two reasons you could want to use a tree: You want to mirror the problem using a tree-like structure:For this we have boost graph library Or you want a container that has tree like access characteristics For this we have std::map (and std::multimap) std::set (and std::multiset) Basically the characteristics of these two containers is such that they practically … Read more

In C++, what is wx.h?

What is wx.h It is the header file of a library. The GitHub project should have some instructions on how to fetch the dependencies and how to install them. How do I install it in my compiler so that I can use it? Normally you have to download the dependency, build it (following some instructions), … Read more

How can I create objects while adding them into a vector?

To answer the first part of your question, you must create an object of type Player before you can use it. When you say push_back(Player), it means “add the Player class to the vector”, not “add an object of type Player to the vector” (which is what you meant). You can create the object on the stack like … Read more