【问题标题】:Incorrect Error Squiggles In Visual Studio CodeVisual Studio 代码中不正确的错误曲线
【发布时间】: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


【解决方案1】:

Rectangle 内部没有字段 width - 您有字段 shape。想必你想做的是rect->shape.width = 100

请注意,您的代码不会按原样运行,因为您从未为 rect 分配内存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 2011-02-27
    • 2013-09-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多