我希望能够创建一个包含3个浮点数(x,y,z)的类型。我尝试过:
typedef struct
{
float x;
float y;
float z;
} Vertex;
但是那并没有起作用。
这个需要在可以被 main
看到的地方声明吗?我该如何为我创建的类型创建 getter 方法和其他方法?
我希望能够创建一个包含3个浮点数(x,y,z)的类型。我尝试过:
typedef struct
{
float x;
float y;
float z;
} Vertex;
但是那并没有起作用。
这个需要在可以被 main
看到的地方声明吗?我该如何为我创建的类型创建 getter 方法和其他方法?
我会在C++中这样做。请参见main()的示例用法。注意:这未经编译或测试。
#include <iostream>
class Vertex
{
public:
// Construction
Vertex(float x,float y, float z) : x_(x), y_(y), z_(z) {}
// Getters
float getX() const {return x_;}
float getY() const {return y_;}
float getZ() const {return z_;}
// Setters
void setX(float val) {x_ = val;}
void setY(float val) {y_ = val;}
void setZ(float val) {z_ = val;}
private:
float x_;
float y_;
float z_;
};
int main()
{
Vertex v(6.0f,7.2f,3.3f);
v.setZ(7.7f);
std::cout < "vertex components are " << v.getX() << ',' << v.getY() << ',' << v.getZ() << std::endl;
}
struct Point3d { double x, y, z; }
更优秀? - Alexandre C.使用C语言,这对我很有效
typedef struct { float x; float y; float z; } Vertex;
int main(void) {
Vertex a = {42, -42, 0};
if (a.x + a.y + a.z == 0) return 1; /* warning about comparing floating point values */
return 0;
}
#include
引入。
struct Vertex { float x, y, z; };
。 - Alexandre C.