【发布时间】:2021-08-27 07:29:50
【问题描述】:
我目前正在编写一个 C 程序,其结构名为 Rectangle。下面的代码显示了我是如何设置它的。
typedef struct {
int width;
int height;
int x;
int y;
} Rectangle;
之后,我创建了另一个名为Ellipse 的结构,它包含与Rectangle 结构完全相同的值,以及更多。
我想保持我的代码整洁,所以我决定创建一个名为Shape 的基本结构。
我在其他结构的标题中使用了这个Shape 结构。
typedef struct {
int width;
int height;
int x;
int y;
} Shape;
typedef struct {
Shape shape;
} Rectangle;
typedef struct {
Shape shape;
int angle;
int anchor_x;
int anchor_y;
} Ellipse;
现在,每当我创建这些结构之一的实例时,VSCode 都会认为有错误。
Rectangle *rect;
rect -> width = 100;
VSCode 在width 下放置了一个错误曲线,上面写着struct "<unnamed>" has no field "width",但是当我编译代码时,它运行良好。
为什么 VSCode 会引发此错误?我应该以不同的方式做事吗?我怎样才能阻止它做出不必要的曲线?
请耐心等待,我是 C 编程新手。
【问题讨论】:
-
请注意,IDE 的“曲线”通常独立于编译器将产生的错误和警告。
标签: c visual-studio-code struct