错误:struct没有名为X的成员

时间:2013-11-17 17:03:38

标签: c struct tree binary-tree

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

 struct treeNode
 {
    *char word;
    int NumberCnt; 
    struct treeNode *rightPTR, *leftPTR; 

 };
 typedef struct treeNode node;

  node *rootPTR = NULL;

 void freeTree(node *currPTR)
 {
     if (currPTR!= NULL)
    {
        freeTree(currPTR -> leftPTR);
        free(currPTR);
        freeTree(currPTR -> rightPTR);
    }
 }

void printTree(node *currPTR)
{
    if (currPTR != NULL)
        {
            printTree(currPTR ->leftPTR);   
            printf("%d\n", currPTR->word);
            printTree(currPTR ->rightPTR);  
        }
}



int insertNode (char* input)
{

    node *tempPTR = malloc(sizeof(node));
    tempPTR -> word = input;
    tempPTR -> NumberCnt=0;
    tempPTR -> leftPTR = NULL;
    tempPTR -> rightPTR = NULL;

    if (rootPTR == NULL)
    {   
        rootPTR = tempPTR;
        rootPTR -> NumberCnt++;
    }
     else 
    {
        int comp;
        node *currPTR = rootPTR;
        node *prevPTR = NULL;

            while (currPTR != NULL)
            {
                comp = strcmp(input, (currPTR->word));
                if (comp = 0)
                {
                    printf ("Entry already exists");
                    return 1;   
                }
                else if (comp < 0)
                {
                    prevPTR = currPTR;
                    currPTR = currPTR->leftPTR;
                }
                else if (comp > 0)
                {
                    prevPTR = currPTR;
                    currPTR = currPTR->rightPTR;
                }

            }

        comp = strcmp(input, (prevPTR ->word));
        if (comp < 0)
        {
            prevPTR->leftPTR = tempPTR;

        }

        else if (comp > 0)
        {
            prevPTR->rightPTR = tempPTR;

        }

        return 0;   
    }

    return 2;
}

int search(char* input) 
{
     if (input == rootPTR ->data)
    {
        printf("Node found %d\n", rootPTR->data);
        return 0;
    }
    else
    {
        if (input < rootPTR ->data)
            {

                node *currPTR = rootPTR->leftPTR;

                while (currPTR != NULL)
                 {
                    if (input == currPTR->data)
                    {
                        printf("Node found %d\n", currPTR->data);
                         return 0;
                     }
                    else if (input < currPTR->data)
                    {
                        currPTR = (currPTR -> leftPTR); 
                    }
                    else if (input > currPTR->data)
                    {
                        currPTR = (currPTR -> rightPTR);
                     } 
                }
                printf ("Node not in tree\n");
                return 1;
            }

             if (input > rootPTR ->data)
            {

                node *currPTR = rootPTR->rightPTR;

                while (currPTR != NULL)
                {

                    if (input == currPTR->data)
                    {
                        printf ("Node found %d\n", currPTR->data);
                        return 0;
                    }

                    else if (input < currPTR->data)
                    {
                        currPTR = (currPTR -> leftPTR); 
                    } 

                    else if (input > currPTR->data)
                    {
                        currPTR = (currPTR ->rightPTR);
                    }
                }
                printf ("Node not in tree\n");
                return 1;
            }

    }

return 2;
}

void fixWord(char* buff)
{
    char* unfixed = buff;
    char* fixed = buff;

    while (*unfixed)
    {

            if (isalpha(*unfixed))
        {
            *fixed=tolower(*unfixed);
                *fixed++;

        }   
            *unfixed++;


    }
    *fixed=0;

}


int main()
{   
    FILE *ptr_file;
    char buff [100];
  //ptr_file = fopen ("sherlock.txt", "r");
    ptr_file = fopen ("input.txt", "r");
    if (!ptr_file)
        printf("File read error");


        while(fscanf(ptr_file, "%s ", buff ) != EOF)
        {
            int comparison = strcmp(buff, "endoffile");
            if (comparison == 0)
            {
                return 0;
            }

             fixWord(buff);
             insert(buff);
        }

    fclose(ptr_file);

}

我有这个代码,它是一个二进制树,从文件中读取文本,然后将其添加到二叉树中。我有一个结构来表示一个新节点,它接受一个字符串和一个增量显示字数的整数。我最初设置这个树以接受整数来测试树的功能,它工作得很好,但是因为更新我的struct以接受字符串和增加的整数,编译器抱怨结构不再包含任何成员结构。我不知道为什么会这样。

2 个答案:

答案 0 :(得分:2)

查看编译器的 first 错误消息。它应该抱怨第6行中的*char,它应该是char *

顺便说一句:始终复制并粘贴错误消息,以便我们获取原始消息。

答案 1 :(得分:0)

您尝试在data函数中的多个位置使用rootPTR ->data成员search,但struct treeNode没有名为data的成员。