C语言 错误:结构没有名为 X 的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20033539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: struct has no member named X
提问by Shaun1810
#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);
}
I have this code which is a binary tree which reads in text from a file and then adds it to a binary tree. I have a struct to represent a new node which takes in a string and a integer which increments to show word count. I originally had this tree set up to take in integers to test the tree functionality and it worked just fine, however since updating my struct to take in the string and the incremented integer the compiler is complaining the struct does not longer contains any of the members of the struct. I have no idea why this happening.
我有这个代码,它是一个二叉树,它从文件中读取文本,然后将其添加到二叉树中。我有一个结构来表示一个新节点,该节点接受一个字符串和一个整数,该整数递增以显示字数。我最初将这棵树设置为接收整数来测试树的功能,它工作得很好,但是由于更新我的结构以接收字符串和递增的整数,编译器抱怨该结构不再包含任何成员的结构。我不知道为什么会发生这种情况。
采纳答案by Roland Illig
Look at the firsterror message of the compiler. It should complain about the *charin line 6, which should be char *.
看编译器的第一条错误信息。它应该抱怨*char第 6 行中的char *.
By the way: always copy and paste error messages, so that we get the original messages.
顺便说一句:总是复制和粘贴错误消息,以便我们获得原始消息。
回答by jpw
You're trying to use a member datalike rootPTR ->dataat several places in the searchfunction but the struct treeNodehas no member called data.
你试图用一个成员data喜欢rootPTR ->data在在几个地方search功能,但struct treeNode没有所谓的成员data。

