c - 错误 C2440 : 'function' : cannot convert from 'node *' to 'node'

标签 c

当我尝试运行该程序时出现此错误:

Error C2440: 'function' : cannot convert from 'node *' to 'node'

当我以 first_node 作为参数调用方法 destroy_node 时,我不知道我做错了什么。

int dequeue(int *data)
{
    node *first_node;
    int result = 0;

    if(queueref.first != NULL){
        first_node = queueref.first;
        data = (int*) queueref.first->data;

        queueref.first = first_node->next;

        result = destroy_node(first_node);
    }

    return result;
}

static int destroy_node(node *node_ref)
{
    int data = 0;

    free(node_ref);

    if(node_ref == NULL){
        data = 1;
    }

    return data;
}

感谢您的帮助!

编辑:

整个类及其给出错误的代码:result = destroy_node(first_node);

    #include <stdlib.h>
#include <stdio.h>

typedef struct node {
    int data;
    struct node *next;
} node;

typedef struct {
    struct node *first;
    struct node *last;
} queue_c;

static queue_c queueref;

//Funktionsprototyper
#include "queue.h"
static int destroy_node(node node_ref);
static node *create_node(int data);

int enqueue(int data)
{
    node *new_node, *last_node;
    int result = 0;

    new_node = create_node(data);
    if(new_node != NULL){
        if(queueref.first != NULL){
            queueref.first = new_node;
            queueref.last = new_node;
        }
        else{
            queueref.last->next = new_node;
            queueref.last = new_node;
        }

        result = 1;
    }

    return result;
}

static node *create_node(int data)
{
    node *noderef;
    noderef = (node*) malloc(sizeof(node));
    noderef->data = data;
    noderef->next = NULL;

    return noderef;
}

int dequeue(int *data)
{
    node *first_node;
    int result = 0;

    if(queueref.first != NULL){
        first_node = queueref.first;
        data = (int*) queueref.first->data;

        queueref.first = first_node->next;

        result = destroy_node(first_node);
    }

    return result;
}

static int destroy_node(node *node_ref)
{
    free(node_ref);
    return node_ref == NULL;
}

void print_queue()
{
    node *curr_ptr = queueref.first;

    if(curr_ptr != NULL){
        while(curr_ptr != NULL){
            printf("%d -> ", curr_ptr->data);
            curr_ptr = curr_ptr->next;
        }
    }
    else{
        printf("Kön är tom!");
    }
}

int delete_node(int data)
{
    int result = 0;

    node *curr_ptr;
    node *prev_ptr;
    node *temp_ptr;

    if(queueref.first == NULL){
        printf("Kön är tom!");
    }
    else if(queueref.first->data == data){
        temp_ptr = queueref.first;
        queueref.first = queueref.first->next;
        result = destroy_node(temp_ptr);
    }
    else{
        prev_ptr = queueref.first;
        curr_ptr = queueref.first->next;

        while(curr_ptr != NULL){
            if(curr_ptr->data == data){
                result = 1;
                break;
            }

            prev_ptr = curr_ptr;
            curr_ptr = curr_ptr->next;
        }

        if(result){
            temp_ptr = curr_ptr;
            prev_ptr->next = temp_ptr->next;
            result = destroy_node(temp_ptr);
        }
        else{
            printf("Ingen node innehöll talet: %d", data);
        }
    }

    return result;
}

编辑:

放松一下,我忘了把原型(prototype)放在 queue.h 中,我在执行程序时错过了它。

最佳答案

原型(prototype)可能有冲突;所示代码不应生成此错误。但它也应该至少产生一个警告,因为你正在调用一个未知函数;将所有 destroy_node() 放在调用它的地方之前,或者添加一个原型(prototype)会更有意义。

请注意,destroy_node() 函数不必要地复杂。它可以重写为:

static int destroy_node(node *node_ref)
{
  free(node_ref);
  return node_ref == NULL;
}

关于c - 错误 C2440 : 'function' : cannot convert from 'node *' to 'node' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8310603/

相关文章:

c - C 中 char[] 和 char * 的区别

c - 读取并存储 C 字符串值

c++ - 你如何处理NUL?

c - 为什么最右边的数字不是零(C/Linux)?

c - 为什么 linux 编译的程序不能在 Windows 上运行

c - 直接将 char *nib 转换为 int hex

c++ - 在命令提示符和 PowerShell 中运行 C++ 程序的区别

c - 在c中编辑字符串数组

c - 在c中重定向标准输出然后重置标准输出

c - 将 char* 添加到链表不起作用