-4 c++ visualization double-precision
我试图用c ++创建我的第一堂课.我正在调用文件geometryitems.cpp并执行此操作:
using namespace std;
class Point
{
double x, y, z;
public:
// constructor
Point(double x, double y, double z)
{
}
// copy constructor
Point(const Point& pnt)
{
x = pnt.x;
y = pnt.y;
z = pnt.z;
}
void display()
{
cout << "Point(" << x << ", " << y << ", " << z << ")";
}
};
Run Code Online (Sandbox Code Playgroud)
然后我从另一个文件中调用它:
#include <iostream>
#include <cstdio>
#include "geometryitems.cpp"
using namespace std;
int main()
{
// initialise object Point
Point pnt = Point(0, 0, 0);
cout << "Point initialisation:" << endl;
pnt.display();
double t = 0;
cout << endl << t << endl;
Point pnt2 = pnt;
pnt2.display();
// keep the terminal open
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是输出:

t显示为正常0但其他零则是其他一些数字.我会理解,如果它们只是非常小的数字,但也有非常大的......
为什么零Point显示为如此奇怪的数字?有没有办法让它看起来像普通的零?
您没有将成员变量设置为构造函数中的任何值:
Point(double x, double y, double z)
{
}
Run Code Online (Sandbox Code Playgroud)
你需要
Point(double x, double y, double z) : x(x), y(y), z(z) {}
Run Code Online (Sandbox Code Playgroud)
这将初始化您的数据成员x,y并z使用构造函数初始化列表.
您还应该删除您的复制构造函数.编译器合成的一个会做得很好.
| 归档时间: |
|
| 查看次数: |
87 次 |
| 最近记录: |