当struct具有该成员时没有成员错误。编译器忽略声明

时间:2012-03-10 00:04:00

标签: c

我遇到了c中的错误,其中编译器给出了以下错误:

在你看到这些错误之前,只要意识到他们大多数要么说我有未申报的东西然后给出我的结构名称,要么说我的structname没有名为“head”或“tail”的成员......然而我的结构确实有这些成员......

File.c:31: error: expected specifier-qualifier-list before ‘LINKNODE’
File.c: In function ‘f1d’:
File.c:78: warning: passing argument 4 of ‘fwrite’ makes pointer from integer without a cast
File.c: In function ‘f2’:
File.c:86: error: called object ‘"ls -al "’ is not a function
File.c: In function ‘f4’:
File.c:115: error: ‘NLIST’ has no member named ‘head’
File.c:116: error: ‘LINKNODE’ undeclared (first use in this function)
File.c:116: error: (Each undeclared identifier is reported only once
File.c:116: error: for each function it appears in.)
File.c:116: error: ‘starter’ undeclared (first use in this function)
File.c:116: error: expected expression before ‘)’ token
File.c:118: error: ‘NLIST’ has no member named ‘head’
File.c:119: error: ‘Linklist’ undeclared (first use in this function)
File.c:121: error: ‘NODE’ undeclared (first use in this function)
File.c:121: error: ‘ptr’ undeclared (first use in this function)
File.c:121: error: ‘NLIST’ has no member named ‘head’
File.c:122: error: ‘NLIST’ has no member named ‘tail’
File.c:126: error: expected ‘;’ before ‘NewNode’
File.c:127: error: ‘NewNode’ undeclared (first use in this function)
File.c:129: error: ‘NLIST’ has no member named ‘tail’
File.c: In function ‘f5a’:
File.c:168: error: ‘LINKNODE’ undeclared (first use in this function)
File.c:168: error: ‘NewNode’ undeclared (first use in this function)
File.c:168: error: expected expression before ‘)’ token
File.c:170: error: ‘list’ undeclared (first use in this function)
File.c: In function ‘f5b’:
File.c:175: error: ‘NLIST’ has no member named ‘head’
File.c:175: error: ‘NLIST’ has no member named ‘head’
File.c: In function ‘printList’:
File.c:186: error: ‘LINKNODE’ undeclared (first use in this function)
File.c:186: error: ‘ptr’ undeclared (first use in this function)
File.c:186: error: ‘NLIST’ has no member named ‘head’
File.c: In function ‘createList’:
File.c:195: error: ‘LINKNODE’ undeclared (first use in this function)
File.c:195: error: ‘ptr’ undeclared (first use in this function)
File.c:195: error: expected expression before ‘)’ token
File.c:196: error: ‘NLIST’ has no member named ‘head’
File.c:198: error: ‘nextone’ undeclared (first use in this function)
File.c:198: error: expected expression before ‘)’ token
File.c:203: error: ‘NLIST’ has no member named ‘tail’

我的代码确实声明了这些对象,并且对象确实有那些关联的成员。它必须是一些我忽略的简单语法错误。这是我的代码:

#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */
#include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */
#include <stdint.h>
#include <sys/types.h>
#include <fcntl.h>

#define ECHOMAX 255     /* Longest string to echo */

int sock;                        /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
struct sockaddr_in fromAddr;     /* Source address of echo */
unsigned short echoServPort;     /* Echo server port */
unsigned int fromSize;           /* In-out of address size for recvfrom() */
char *servIP;                    /* IP address of server */
char *echoString;                /* String to send to echo server */
char echoBuffer[ECHOMAX+1];      /* Buffer for receiving echoed string */
int echoStringLen;               /* Length of string to echo */
int respStringLen;               /* Length of received response */


typedef struct node { 
    int storage;
    struct node *next;
} lINKNODE;

typedef struct nlist {
    LINKNODE *head;
    LINKNODE *tail;
} NLIST;

char *create_substring(char *s, int first, int last)
{
    int length = (last-first)+1;
    char *new_str = malloc(length+1);  //need an extra byte for the 0.
    strncpy(new_str, s+first,length);
    new_str[length] = '\0';
    return new_str;
}

void f1a(char *filename,char *string){
    FILE *writeto=(FILE *)fopen(filename,"a"); //fixed length? huh?
    fprintf(writeto,"%s",string);
    fclose(writeto);
}

void f1b(char *filename,int k){
    FILE *readfrom=(FILE *)fopen(filename,"r");
    char *c=malloc(1);
    int i;
    if (read==NULL){
        fprintf(stderr,"file not opened \n");
        return;
    }
    for (i=1;i<k-1;i++){
        fgets(c,1,readfrom);
    }
    fclose(readfrom);
}//change

void f1c(char *filename,int k){
    FILE *readfrom=(FILE *)fopen(filename,"r");
    fseek(readfrom,k,0); //third argument... 0??? whence? what? 
    char *c;
    fgets(c,1,readfrom);
    printf("The kth character is %s\n",c);
    fclose(readfrom);   //read the kth character? print character
}//offset t? return value?

void f1d(char *filename,int k, char *sentence){
    FILE *readfrom=(FILE *)fopen(filename,"rw");
    if (readfrom!=NULL){
        int k=fseek(readfrom,k,0);
        int f=strlen(sentence);
        fwrite(sentence,1,f,k);
        fclose(readfrom);}
    else {
        printf("File could not be opened\n");
    }
}
void f2(char *F,char *D){
    char *command="ls -al "
    strcat(command,D);
    strcat(command," | grep -E \"^([& ]+ +){7}.*");
    strcat(command,F);
    strcat(command,"\" > t1.out");
    system (command);
    char *filename="t1.out";//error check as he said
    FILE *readfrom=(FILE *)fopen(filename,"r");
    char *currentline=malloc(200);
    if (readfrom!=NULL){
        char *sentence=malloc(200);
        while (fgets(currentline,200,readfrom)!=NULL){
            printf("%s",currentline);
        }
    }
}

void f3(){
    int myArray[1000];
    int i;
    for (i=0;i<1000;i++){
        myArray[i]=rand();
    }
    int sum=0;
    for (i=0;i<1000;i++){
        sum+=myArray[i];
    }
    printf("The array sum is %i\n",sum);
}
void f4(NLIST *LinkList){
    if (LinkList->head == NULL){
        LINKNODE *starter=(LINKNODE *)malloc(sizeof(LINKNODE));
        starter->storage=100;
        LinkList->head=starter;
        Linklist->tail=starter;
    }
    NODE *ptr=LinkList->head;
    while (ptr != LinkList->tail){
        ptr=ptr->next;
    }

    LINKNODE NewNode=(LINKNODE *)malloc(sizeof(LINKNODE));
    NewNode->storage=0;
    Linklist->tail->next=NewNode;
    LinkList->tail=NewNode;
}







void initsocket(int argc, char *argv[]){
    if ((argc < 3) || (argc > 4))    /* Test for correct number of arguments */
    {
        fprintf(stderr,"Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n", argv[0]);
        exit(1);
    }

    servIP = argv[1];           /* First arg: server IP address (dotted quad)*/
    echoString = argv[2];       /* Second arg: string to echo */

    if ((echoStringLen = strlen(echoString)) > ECHOMAX)  /* Check input length */
        printf("Echo word too long");

    if (argc == 4)
        echoServPort = atoi(argv[3]);  /* Use given port, if any */
    else
        echoServPort = 7;  /* 7 is the well-known port for the echo service */

    /* Create a datagram/UDP socket */
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        printf("socket() failed");

    /* Construct the server address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));    /* Zero out structure */
    echoServAddr.sin_family = AF_INET;                 /* Internet addr family */
    echoServAddr.sin_addr.s_addr = inet_addr(servIP);  /* Server IP address */
    echoServAddr.sin_port   = htons(echoServPort);     /* Server port */
               }

void f5a(NLIST *LinkList){
    LINKNODE *NewNode=(LINKNODE *)malloc(sizeof(NODE));
    NewNode->storage=1231231;
    list->tail->next=NewNode;
    list->tail=NewNode;
}

void f5b(NLIST *LinkList){
    LinkList->head=LinkList->head->next;
}


void f6(){
    if (sendto(sock, echoString, echoStringLen, 0, (struct sockaddr *)
               &echoServAddr, sizeof(echoServAddr)) != echoStringLen)
        printf("sendto() sent a different number of bytes than expected\n");
}

void printList(NLIST *LinkList){
    LINKNODE *ptr=LinkList->head;
    while (ptr !=NULL){
        printf("%i\n",ptr->storage);
        ptr=ptr->next;
    }
}

void createList(NLIST *LinkList){
    int i=0;
    LINKNODE *ptr=(LINKNODE *)malloc(sizeof(LINKNODE));
    LinkList->head=ptr;
    for (i=0;i<1000;i++){
        LINKNODE *nextone=(LINKNODE *)malloc(sizeof(LINKNODE));
        ptr->storage=i;
        ptr->next=nextone;
        ptr=nextone;
    }
    LinkList->tail=ptr;
}





int main(int argc,char *argv[]){
    printf("%c",flb("Untitled 2.txt",5));
    char *sentence=malloc(200);
    FILE *readfrom=(FILE *)fopen("Untitled 2.txt","r");

    fclose(readfrom);
}

4 个答案:

答案 0 :(得分:5)

您的代码错误。仅举几例:

  • 您正在创建typedef lINKNODE而不是LINKNODE
  • char *command="ls -al "
  • 之后你错过了一个分号
  • 您使用Linklist代替LinkList
  • 您说NewNode->storage=0;其中NewNode不是指针

答案 1 :(得分:4)

解决了所有语法问题后,您会发现由于strcat的使用错误,您的程序会出现段错误。

strcat要求目标足够大以容纳连接字符串的结果。现在你做:

char *command = "ls -al ";

这实际上会声明一个字符串文字,通常放入只读内存中。当你尝试向它添加一些东西时,这是一个写入,它将失败。即使你愿意:

char command[] = "ls -al ";

这仍然只为字符串“ls -al”保留足够的内存,并且附加到它将导致奇怪的错误,并且可能会在某个随机点发生段错误,因为您将覆盖堆栈内容。您需要使用strlen来查找所需的内存量,然后使用malloc进行分配,然后使用strcpy复制所有值。

答案 2 :(得分:3)

您尚未定义名为LINKNODE的类型。您已定义了一个名为lINKNODE的类型,其中包含小写字母l

这只是第一次报告错误。

当您收到类似这样的级联错误消息时,请仔细检查第一个错误消息。其余的可能是编译器混淆的副作用。修复第一个错误,重新编译,迭代。 (并且要小心改变只是为了让编译器满意;编译的代码不一定正确。)

答案 3 :(得分:3)

第一个错误是因为您声明了lINKNODE的typedef,但您可能打算LINKNODE。即,第一个字母没有正确地大写。