在C+;中创建一个具有两个双值的类+; 在C++中,我试图创建一个包含两个双值的类Posit2D。所有数据成员和函数都应该是公共的
对于公众成员,应该有在C+;中创建一个具有两个双值的类+; 在C++中,我试图创建一个包含两个双值的类Posit2D。所有数据成员和函数都应该是公共的,c++,class,object,C++,Class,Object,对于公众成员,应该有 双x 双y 建造商 默认构造函数应将x和y初始化为0.0 点2D(双入x,双入y) 将x和y设置为in_x和in_y 对于非成员函数 void GetResult(点2D p1、点2D p2) 打印两者的x和y值 这是我目前掌握的代码,有人能指出我的错误吗 点2D.h #ifndef POINT2D_H #define POINT2D_H class Point2D { public: double x; double y; Poin
- 双x
- 双y
- 默认构造函数应将x和y初始化为0.0
- 点2D(双入x,双入y)
- 将x和y设置为in_x和in_y
- void GetResult(点2D p1、点2D p2)
- 打印两者的x和y值
#ifndef POINT2D_H
#define POINT2D_H
class Point2D
{
public:
double x;
double y;
Point2D();
Point2D(double,double);
};
void GetResult(Point2D, Point2D);
#endif
Point2D.cpp
#include "Point2D.h"
#include <iostream>
using namespace std;
Point2D::Point2D()
{
x = 0.0;
y = 0.0;
}
Point2D::P1(double in_x, double in_y)
{
x = in_x;
y = in_y;
}
Point2D::P2(double in_x, double in_y)
{
x = in_x;
y = in_y;
}
void GetResult(Point2D P1, Point2D P2)
{
cout << P1.x << " " << P1.y << endl;
cout << P2.x << " " << P2.y << endl;
}
#包括“Point2D.h”
#包括
使用名称空间std;
Point2D::Point2D()
{
x=0.0;
y=0.0;
}
点2D::P1(双入x,双入y)
{
x=in_x;
y=in_y;
}
点2D::P2(双入x,双入y)
{
x=in_x;
y=in_y;
}
void GetResult(点2D P1、点2D P2)
{
cout您很接近,但很明显您对重载构造函数和类的声明实例有一点误解。对于初学者,您不需要函数:
Point2D::P1(double in_x, double in_y)
{
x = in_x;
y = in_y;
}
Point2D::P2(double in_x, double in_y)
{
x = in_x;
y = in_y;
}
只需为Point2D
类提供一个构造函数,该构造函数接受两个双值,例如
Point2D::Point2D(double in_x, double in_y)
{
x = in_x;
y = in_y;
}
然后在main()
#include <iostream>
#include "Point2D.h"
using namespace std;
int main()
{
Point2D Point1 (1.0, 2.0);
Point2D Point2 (1.0, 2.0);
GetResult(Point1, Point2);
}
及
注意:根本不需要在main()
中使用名称空间std;
,而且您真的不应该在任何地方包含整个标准名称空间。只需删除两个调用,并将std::
添加到两个cout
调用和两个endl
(或者只使用'\n'
而不是std::endl;
)。请参阅
相反,只需使用:
void GetResult(Point2D P1, Point2D P2)
{
std::cout << P1.x << " " << P1.y << '\n';
std::cout << P2.x << " " << P2.y << '\n';
}
void GetResult(点2D P1、点2D P2)
{
问题是什么?…如果您编译了代码并得到错误,错误消息是什么?(将它们附加到问题中,而不是在注释中)不知道P1和P2函数是什么,它们看起来像是丢失构造函数的两个错误命名的副本。您可以考虑使用命名空间STD避免<代码>;也可能指出当前双倍没有被初始化。它们是默认构造的,然后被分配给它们。它们应该在构造函数INI中初始化。tialization列表。从基于问题的学习角度来看,在包含了关于使用初始值设定项列表的说明之后,默认构造函数可能是消除混淆的最简单的介绍或方法。C++11初始值设定项列表增加了灵活性,但也增加了设置成员值的额外层复杂性现在我们重新讨论。构造函数初始化列表-如Point2D():x(0),y(0){};
在C++11中不是新的,它从C++98开始就存在了。我只是按照注释:(2)进行说明在CPPPreference链接中。我不确定它何时出现。我从Borland基于DOS时开始…我将删除它出现时的引用。谢谢。现在我必须去查阅98简介。@JesperJuhl-你确定吗?CPPPreference很少出错,他们的注释(2)在我开始回顾过去的标准之前,你有没有一个你认为可以节省时间的参考资料?
#ifndef POINT2D_H
#define POINT2D_H
class Point2D
{
public:
double x;
double y;
Point2D();
Point2D(double,double);
};
void GetResult(Point2D, Point2D);
#endif
#include "Point2D.h"
#include <iostream>
using namespace std;
Point2D::Point2D()
{
x = 0.0;
y = 0.0;
}
Point2D::Point2D(double in_x, double in_y)
{
x = in_x;
y = in_y;
}
void GetResult(Point2D P1, Point2D P2)
{
cout << P1.x << " " << P1.y << endl;
cout << P2.x << " " << P2.y << endl;
}
$ ./bin/TestCheckPoint1
1 2
1 2
void GetResult(Point2D P1, Point2D P2)
{
std::cout << P1.x << " " << P1.y << '\n';
std::cout << P2.x << " " << P2.y << '\n';
}