【问题标题】:Error 'a value of type "X *" cannot be assigned to an entity of type "X *"' when using typedef struct使用 typedef 结构时,错误“无法将类型为“X *”的值分配给类型为“X *”的实体
【发布时间】:2016-08-01 13:49:36
【问题描述】:

这是我用于节点的结构...

typedef struct
{
    struct Node* next;
    struct Node* previous;
    void* data;
} Node;

这是我用来链接它们的函数

void linkNodes(Node* first, Node* second)
{
    if (first != NULL)
        first->next = second;

    if (second != NULL)
        second->previous = first;
}

现在视觉工作室在这些行上给了我智能感知(更少)错误

IntelliSense: a value of type "Node *" cannot be assigned to an entity of type "Node *"

谁能解释这样做的正确方法? Visual Studio 将编译并运行它,它也可以在我的 mac 上运行,但在我的学校服务器上崩溃了。

编辑:我想过使用 memcpy 但这很简单

【问题讨论】:

  • 您在此处报告的错误消息是“错误的”,应该是"Node *" cannot be assigned to an entity of type "struct Node *"。要么你有一个非常糟糕的编译器,它会混淆事情(不太可能),这表明你以某种方式碰巧将你的代码编译为 C++。不要那样做,C 和 C++ 是不同的语言,你的问题就是一个很好的例子。

标签: c struct compiler-errors typedef


【解决方案1】:

我觉得问题是没有struct叫Node,只有typedef。试试

 typedef struct Node { ....

【讨论】:

    【解决方案2】:

    与 Deepu 的回答类似,但可以让您的代码编译的版本。将您的结构更改为以下内容:

    typedef struct Node // <-- add "Node"
    {
        struct Node* next;
        struct Node* previous;
        void* data;
    }Node; // <-- Optional
    
    void linkNodes(Node* first, Node* second)
    {    
        if (first != NULL)
            first->next = second;
    
        if (second != NULL)
            second->previous = first;
    }
    

    【讨论】:

      【解决方案3】:

      在 C 中定义 typedefstruct 最好在 struct 声明本身之前完成。

      typedef struct Node Node; // forward declaration of struct and typedef
      
      struct Node
      {
          Node* next;          // here you only need to use the typedef, now
          Node* previous;
          void* data;
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-10
        • 1970-01-01
        • 2020-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多