简体   繁体   English

使用typedef结构时,错误'类型为“X *”的值无法分配给“X *”类型的实体

[英]Error 'a value of type “X *” cannot be assigned to an entity of type “X *”' when using typedef struct

Here is the struct I am using for the nodes... 这是我用于节点的结构...

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

and here is the function I am using to link them 这是我用来链接它们的功能

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

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

now visual studio is giving me the intellisense(less) error on those lines 现在,visual studio在这些行上给了我intellisense(less)错误

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

can anyone explain the proper way to do this? 任何人都可以解释这样做的正确方法吗? Visual studio will compile it and run it find and it also works on my mac but is crashing on my schools servers. Visual Studio将编译它并运行它查找它也可以在我的Mac上运行但是在我的学校服务器上崩溃。

edit: i thought of using memcpy but that's pretty cheasy 编辑:我想使用memcpy,但这很可怕

I think the problem is there is no struct called Node, there is only a typedef. 我认为问题是没有名为Node的结构 ,只有一个typedef。 Try 尝试

 typedef struct Node { ....

Similar to Deepu's answer, but a version that will let your code compile. 与Deepu的答案类似,但是会让您的代码编译的版本。 Change your struct to the following: 将结构更改为以下内容:

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;
}

Defining typedef of struct in C is best done before the struct declaration itself. 在C语言中定义struct typedef最好在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;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM