我被限制在比赛中使用 C,我需要模拟课程。我正在尝试构建一个简单的“点”类,它可以返回并设置一个点的 X 和 Y 坐标。然而,下面的代码返回诸如“未知类型名称点”、“预期标识符或(”和“预期参数声明符”等错误。这些错误是什么意思?我该如何纠正它们?这是编写“的正确方法”伪类”?
typedef struct object object, *setCoordinates;
struct object {
float x, y;
void (*setCoordinates)(object *self, float x, float y);
void (*getYCoordinate)(object *self);
void (*getXCoordinate)(object *self);
};
void object_setCoordinates(object *self, float x, float y){
self->x = x;
self->y = y;
}
float object_getXCoordinate(object *self){
return self->x;
}
float object_getYCoordinate(object *self){
return self->y;
}
object point;
point.setCoordinates = object_setCoordinates;
point.getYCoordinate = object_getYCoordinate;
point.getXCoordinate = object_getXCoordinate;
point.setCoordinates(&point, 1, 2);
printf("Coordinates: X Coordinate: %f, Y Coordinate: %f", point.getXCoordinate, point.getYCoordinate);
Run Code Online (Sandbox Code Playgroud)
参考: 1. C - struct 内的函数 2.如何在 C 中实现类?
你会做得更好,如下所示:
#include <stdio.h>
struct point {
float x;
float y;
};
void point_setCoordinates(struct point *self, float x, float y){
self->x = x;
self->y = y;
}
float point_getXCoordinate(struct point *self){
return self->x;
}
float point_getYCoordinate(struct point *self){
return self->y;
}
int main(void) {
struct point my_point;
point_setCoordinates(&my_point, 1, 2);
printf("Coordinates: X Coordinate: %f, Y Coordinate: %f\n",
point_getXCoordinate(&my_point),
point_getYCoordinate(&my_point));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
需要注意的几点:
point_*()你在point“事物”上调用的函数。printf()你用point.getXCoordinate-也就是说,你把它的地址和要求printf(),以显示它,就好像它是一个float许多库/API 提供不透明的数据类型。这意味着您可以获得“事物”的“句柄”……但您不知道“事物”中存储了什么。然后该库为您提供访问功能,如下所示。这就是我建议你处理这种情况的方式。
不要忘记释放内存!
我在下面实现了一个例子。
点.h
#ifndef POINT_H
#define POINT_H
struct point;
struct point *point_alloc(void);
void point_free(struct point *self);
void point_setCoordinates(struct point *self, float x, float y);
float point_getXCoordinate(struct point *self);
float point_getYCoordinate(struct point *self);
#endif /* POINT_H */
Run Code Online (Sandbox Code Playgroud)
c点
#include <stdlib.h>
#include <string.h>
#include "point.h"
struct point {
float x;
float y;
};
struct point *point_alloc(void) {
struct point *point;
point = malloc(sizeof(*point));
if (point == NULL) {
return NULL;
}
memset(point, 0, sizeof(*point));
return point;
}
void point_setCoordinates(struct point *self, float x, float y) {
self->x = x;
self->y = y;
}
float point_getXCoordinate(struct point *self) {
return self->x;
}
float point_getYCoordinate(struct point *self) {
return self->y;
}
void point_free(struct point *self) {
free(self);
}
Run Code Online (Sandbox Code Playgroud)
主文件
#include <stdio.h>
#include "point.h"
int main(void) {
struct point *point;
point = point_alloc();
point_setCoordinates(point, 1, 2);
printf("Coordinates: X Coordinate: %f, Y Coordinate: %f\n",
point_getXCoordinate(point),
point_getYCoordinate(point));
point_free(point);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3461 次 |
| 最近记录: |