A class that contains an object of structure vector2d

 class Foo { public: Foo() { acceleration_force = vector2d(); //acceleration_force(); не видит конструктора вообще cout << "acc x: " << acceleration_force.x << " y: " << acceleration_force.y << endl; } private: vector2d acceleration_force; }; 

My vector2d structure

 #ifndef VECTORS_H #define VECTORS_H struct vector2d { vector2d(float y, float x) : y(y), x(x) {} vector2d() : y(-1), x(0) {} float y, x; }; #endif /* VECTORS_H */ 

The output is acc x: inf y: -inf

Although the same code successfully works on ideone

Text output with ncurses function printw .


It looks like I have a problem with pointers and links, because debager shows everything correctly.

AddAcceleration function:

 void AddAcceleration(vector2d direction, float value) { acceleration_force = (direction.normal()*value) * mass; } 

vector2d::normal() function

 vector2d normal() { vector2d result(*this); result.x = result.length() / result.x; result.y = result.length() / result.y; return result; } 

GetForce function:

 vector2d GetForce() { return acceleration_force; } 

vector2d::lenght :

 float length() { return sqrt(y*y+x*x); } 

Foo class variables:

 private: float mass; vector2d acceleration_force; float speed; vector2d friction; vector2d force; vector2d direction; 

GetMass() , GetSpeed() simple getters.

Plot of code that displays vector values:

 foo = Foo(); foo.AddAcceleration(vector2d(-1,0), 2); printw("%f, %f", mov.GetForce().x, mov.GetForce().y); 

I also ask you to look at the classes where I overloaded the operators * and + structures of vector2d . They, too, as I understand, do not work:

 vector2d operator+(const vector2d& right) const { vector2d result(*this); // Make a copy of myself. result.x += right.x; result.y += right.y; return result; } vector2d operator*(const float right) const { vector2d result(*this); result.x *= right; result.y *= right; return result; } 
  • The studio also does not play, maybe the old version is launched and not the current one? - Grundy
  • @grundy studio is not, I'm on Linux. I launch in netbeans , the standard c++11 , gcc 4.8.4 . Project cleaned and recompiled, not working - Herrgott
  • I meant that I ran this code in the studio and everything worked fine, and by the way without the string acceleration_force = vector2d (); too - Grundy
  • g ++ 4.8.4, everything works: acc x: 0 y: -1 . Compiler keys? - PinkTux
  • @pinktux, -lncurses . It looks like I have a problem with pointers, the debager shows everything correctly - Herrgott

1 answer 1

I figured out what the problem is inf , -inf .
When х = 0, y = -1 , the length is sqrt( -1 * -1 + 0 * 0) == 1 , the variable x becomes x = 1 / x = 1 / 0 = inf , then the length is already counted as sqrt ( -1 * -1 * inf * inf) . Plus, on the contrary, it was necessary to divide the coordinate by the length of the vector. And multiplying by mass gives nan , since mass not initialized