Array of object with variables x and y of type double when one equals 1.0000 does not work and gives no error

huangapple go评论65阅读模式
英文:

Array of object with variables x and y of type double when one equals 1.0000 does not work and gives no error

问题

planevector.cpp 文件:

#include <cmath>
#include <string>
using namespace std;
class planeVector
{
    double x;
    double y;
    public:
        planeVector(int x = 0, int y = 0)
        {
            (*this).x = x;
            (*this).y = y;
        }

        ~planeVector()
        {
            delete &x;
            delete &y;
        }

        void operator+=(planeVector vec)
        {
            this->x += vec.x;
            this->y += vec.y;
        }

        void operator-=(planeVector vec)
        {
            this->x -= vec.x;
            this->y -= vec.y;
        }

        void operator*=(double num)
        {
            (*this).x *= num;
            (*this).y *= num;
        }

        void operator/=(double num)
        {
            (*this).x /= num;
            (*this).y /= num;
        }

        planeVector operator+(planeVector vec)
        {
            return planeVector(this->x + vec.x, this->y + vec.y);
        }

        planeVector operator-(planeVector vec)
        {
            return planeVector(this->x - vec.x, this->y - vec.y);
        }

        planeVector operator/(double num)
        {
            return planeVector(this->x / num, this->y / num);
        }

        planeVector operator*(double num)
        {
            return planeVector(this->x * num, this->y * num);
        }
        void normalize()
        {
            (*this) /= (*this).mag();
        }

        double mag()
        {
            return pow(this->x * this->x + this->y * this->y, 0.5); 
        }

        operator std::string const()
        {
            return "(" + std::to_string(this->x) + ", " + std::to_string(this->y) + ")";
        }
};

main.cpp 文件:

#include "string"
#include "iostream"
#include "./planevector.cpp"
using namespace std;

int main()
{
    planeVector y[17];
    for(int i = 0; i < 17; i++)
    {
        y[i] = planeVector(6, 1);
    }
    for(int i = 0; i < 17; i++)
    {
        cout << std::string(y[i]) << endl;
    }
    return 0;
}

如果您遇到了问题,当 x 和 y 的值为 double 类型时无法正常工作,但当它们为 int 或 long 类型时可以正常工作,那么这可能是与浮点数精度相关的问题。浮点数比较可能会受到精度问题的影响,这可能导致在比较时无法正常工作。如果您需要比较浮点数,最好使用容忍度或比较函数来处理精度问题。

英文:

planevector.cpp file

#include &lt;cmath&gt;
#include &lt;string&gt;
using namespace std;
class planeVector
{
double x;
double y;
public:
planeVector(int x = 0,int y = 0)
{
(*this).x = x;
(*this).y = y;
}
~planeVector()
{
delete &amp;x;
delete &amp;y;
}
void operator+=(planeVector vec)
{
this-&gt;x+=vec.x;
this-&gt;y+=vec.y;
}
void operator-=(planeVector vec)
{
this-&gt;x-=vec.x;
this-&gt;y-=vec.y;
}
void operator*=(double num)
{
(*this).x*=num;
(*this).y*=num;
}
void operator/=(double num)
{
(*this).x/=num;
(*this).y/=num;
}
planeVector operator+(planeVector vec)
{
return planeVector((*this).x+vec.x,(*this).y+vec.y);
}
planeVector operator-(planeVector vec)
{
return planeVector((*this).x-vec.x,(*this).y-vec.y);
}
planeVector operator/(double num)
{
return planeVector((*this).x/num,(*this).y/num);
}
planeVector operator*(double num)
{
return planeVector((*this).x*num,(*this).y*num);
}
void normalize()
{
(*this)/=(*this).mag();
}
double mag()
{
return pow(this-&gt;x*this-&gt;x+this-&gt;y*this-&gt;y,0.5); 
}
operator std::string const()
{
return &quot;(&quot;+ std::to_string(this-&gt;x)+&quot;, &quot;+std::to_string(this-&gt;y)+&quot;)&quot;;   
}
};

main.cpp file

#include &quot;string&quot;
#include &quot;iostream&quot;
#include &quot;./planevector.cpp&quot;
using namespace std;
int main()
{
planeVector y[17];
for(int i = 0; i &lt; 17; i++)
{
y[i] = planeVector(6,1);
}
for(int i = 0; i &lt; 17; i++)
{
cout &lt;&lt; std::string(y[i]) &lt;&lt; endl;
}
return 0;
}

whenever I try to create more than one planeVector object with values x = /some number/ and y = 1
it just doesn't work, and there's no error that's given, it just ends. it compiles succesfully and while it runs it stops whenever it reaches the for loop unless the values are not 1.

changing the data types to int or long works, while float and double do not work, however I want it to use double.

答案1

得分: 1

一件立即让我感到奇怪的事情是你的析构函数。xy 并没有使用 new 进行分配,因此删除它们是未定义行为,意味着任何事情都可能发生(最有可能是崩溃)。在99%的C++代码中,你不应该直接调用 delete

尝试移除析构函数,看看会发生什么。

英文:

One thing that immediately strikes me as odd is your destructor. x and y weren't allocated with new, so deleting them is undefined behavior, meaning anything can happen (most likely a crash). You shouldn't have to call delete directly in 99% of C++ code.

Try removing the destructor and see what happens.

huangapple
  • 本文由 发表于 2023年6月13日 11:52:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定