How to avoid gimbal lock (c++)?

Thx! Why does the pitch seem incorrect?