不允许指向不完整类类型的指针

6 c

我正在实现一个冒泡排序功能,它可以对单词进行排序.交换功能字很好,但我无法得到错误.尝试在线搜索,但无法获得有用的东西.我已经标记了我得到错误的地方.

感谢您的帮助.

void sortWord (struct node** head) {
    struct node* temp  = (*head);
    struct node* temp2 = (*head);

    int i;
    int j;
    int counter = 0;
    while(temp != NULL)
    {
        temp = temp->next; //<-- this is where i get the error.
        counter++;
    }
    for( i = 1; i<counter; i++)
    {
        temp2=(*head);
        for(j = 1; j<counter-1;j++)
        {
            if(wordCompare(temp2,nodeGetNextNode(temp2))>0)
            {
                swap(head,temp2,nodeGetNextNode(temp2));
                continue;
            }
        }
        temp2 = nodeGetNextNode(temp2);
    }
}
Run Code Online (Sandbox Code Playgroud)

das*_*ght 12

当您尝试使用struct已向前声明但未定义的语句时,会出现此错误.虽然声明和操作指向这些结构的指针是绝对可以的,但尝试取消引用它们并不行,因为编译器需要知道它们的大小和布局才能执行访问.

具体来说,在您的情况下,编译器不知道struct nodenext,所以

temp->next
Run Code Online (Sandbox Code Playgroud)

不编译.

您需要struct node在编译单元中包含定义sortWord函数的定义,以便解决此问题.

  • 这会起作用,但会影响这段代码的通用性.使用`nodeGetNextNode`代替将允许保持此功能不变.当`node`结构改变时,他只需要更新"回调"(如果我可以在C中调用它们):`nodeGetNextNode`,`wordCompare`和`swap`. (2认同)

Roe*_*rel 5

您应该替换此行:

temp = temp->next;
Run Code Online (Sandbox Code Playgroud)

用那条线:

temp = nodeGetNextNode(temp);
Run Code Online (Sandbox Code Playgroud)

原因是在这段代码中,您对node. 我想这就是你nodeGetNextNode为 temp2使用函数的原因。你只需要将它用于临时。

  • @Manmohit 如果这是您所做的,您应该在此处打勾。并尽快完成……自从您之前检查过它,5 分钟的倒计时开始了。 (2认同)