2014-04-26 55 views
-1

Scheduler.c 我正在爲類進行調度程序,並且我被這個錯誤卡住了。它表示不兼容的類型從'int'類型鍵入'Ev'。我很困惑這可能意味着什麼。不兼容類型從類型'int'在類型50中鍵入'Ev'

int main() 
{ 
Ev *event; 
    int numEvents=0; 
    int option; 
    int x; 
    int y; 

    char menu[]="1 - Insert a new event\n2 - Display all events\n3 - Now?\n4 - Delete expired\n0 - Exit\nPlease select an option: "; 

    event = malloc(sizeof(Ev) *numEvents); 
    CopynumEvents(&numEvents); 
    for (y=0; y<numEvents; y++) 
    { 
     event = realloc(event, sizeof(Ev) * (y + 1)); 



    **MY ERROR SEEMS TO BE AROUND THIS AREA** 

     event[y]=CopyfromFile(numEvents,y); 
    } 

    option = MenuOption(menu); 
    while(1) 
    { 
     switch(option) 
     { 
      case(1):   

       event = realloc(event, sizeof(Ev) * (numEvents + 1)); 
       numEvents++; 
       event[numEvents - 1]=InsertEvent();    
       ArrangeEvents(event,numEvents); 
       CopytoFile(numEvents, event);    
       option = MenuOption(menu);    
       break; 

      case(2): 

       if (numEvents!=0) 
       { 
        for (x=0; x<numEvents; x++) 
        {      
         DisplayEvent(event[x]); 
        } 
       } 
       else 
       { 
        printf("No event in scheduler\n"); 
       } 
       option = MenuOption(menu);    
       break; 

      case(3): 

       ActiveEvents(event, numEvents); 
       option = MenuOption(menu); 
       break; 

      case(4): 

       DeleteExpired(event, &numEvents); 
       CopytoFile(numEvents, event); 
       option = MenuOption(menu); 
       break; 

      case(0): 

       free(event); 
       exit(1);  
       break; 

     } 
    }   
} 

int MenuOption(char *prompt) 
{ 
    char buffer[100]; 
    int option; 

    do 
    { 
     printf("%s", prompt); 

     /* Get a line of up to 100 characters */ 
     fgets(buffer, sizeof(buffer), stdin); 

     /* Remove any \n we may have input */ 
     if(strlen(buffer) > 0) 
      buffer[strlen(buffer)-1] = '\0'; 

     sscanf(buffer, "%d", &option); 

    }while(option!=1 && option!=2 && option!=3 && option!=4 && option!=0); 

    return option; 
} 

struct Event InsertEvent() 
{ 
    struct Event E1; 

    printf("What is the event: ");   
    InputEventname(E1.name, sizeof(E1.name)); 
    E1.date = InputDate("Event date: "); 
    E1.start = InputTime("Start time: ",E1.date); 
    do 
    { 
    E1.end = InputTime("End time: ",E1.date); 
    }while(E1.end<=E1.start); 

    return E1; 
} 

void InputEventname(char *str, int max) 
{ 
    char buffer[100]; 

    /* Get a line of up to 100 characters */ 
    fgets(buffer, sizeof(buffer), stdin); 

    /* Remove any \n we may have input */ 
    if(strlen(buffer) > 0) 
     buffer[strlen(buffer)-1] = '\0'; 

    /* Copy up to max characters to our string */ 
    strncpy(str, buffer, max); 
    str[max-1] = '\0'; 
} 


time_t InputTime(char *prompt, time_t date) 
{ 
    char buffer[100]; 
    char *result; 
    struct tm time; 

    time = *localtime(&date); 

    do 
    { 
     printf("%s", prompt); 

     /* Get a line of up to 100 characters */ 
     fgets(buffer, sizeof(buffer), stdin); 

     /* Remove any \n we may have input */ 
     if(strlen(buffer) > 0) 
      buffer[strlen(buffer)-1] = '\0'; 

     result = strptime(buffer, "%I:%M%p", &time); 

    } while(result == NULL); 

    return mktime(&time); 
} 


time_t InputDate(char *prompt) 
{ 
    char buffer[100]; 
    char *result; 
    struct tm date; 

    do 
    { 
     printf("%s", prompt); 

     /* Get a line of up to 100 characters */ 
     fgets(buffer, sizeof(buffer), stdin); 

     /* Remove any \n we may have input */ 
     if(strlen(buffer) > 0) 
      buffer[strlen(buffer)-1] = '\0'; 

     result = strptime(buffer, "%m/%d/%Y", &date); 

    } while(result == NULL); 

    /* Convert to time_t format */ 
    date.tm_min = 0; 
    date.tm_hour = 0; 
    date.tm_sec = 0; 
    date.tm_isdst = 1; 

    return mktime(&date); 
} 

這是我的標題。我相信我做了這部分的權利

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#include <stdbool.h> 
#include <string.h> 

#define __USE_XOPEN 
#include <time.h> 


#include "scheduler.h" 

/* 

* This is my scheduling program!! 
*/ 


/* Our possible scheduling states */ 
#define NewEvent 1 
#define AllEvents 2 
#define Now 3 
#define Expired 4 
#define Exit 0 




#ifndef DISPLAY_H 
#define DISPLAY_H 

typedef struct Event 
{  
    char name[50];  
    time_t date; 
    time_t start;  
    time_t end; 
int y; 
}Ev; 

int MenuOption(char *prompt); 
struct Event InsertEvent(); 
void InputEventname(char *str, int max); 
time_t InputDate(char *prompt); 
time_t InputTime(char *prompt, time_t date); 
void DisplayEvent(Ev event); 


#endif 
+1

'CopyfromFile'?我們可以研究它嗎? –

+1

這意味着你正試圖將Ev類型的變量設置爲一個int變量。機會是你的CopyFromFile方法返回一個int,而不是Ev,所以'event [y] = CopyfromFile(numEvents,y);'試圖設置事件[y](應該是類型「Ev」)爲值鍵入「int」 – DerStrom8

+0

行50是錯誤號是 – user3574840

回答

0

__USE_XOPEN是內部名稱;它不應該由你設置。您應該使用#define _XOPEN_SOURCE 700或類似的東西(500,600或700)。此外,它應該在之前設置任何包括POSIX頭。標準的C頭文件也是POSIX頭文件。

你應該在問題中給出確切的錯誤信息(除了文件的路徑名,如果它們的長度大於20個字符,將它們截斷爲基本名稱)。信息的關鍵組成部分是行號;您應該在文件中標識該行,除非它是第1行(也許最多可以到第10行,但是如果不標記該行有錯誤,請不要忽略任何行)。

既然你說的編譯器發出警告:

incompatible types to type 'Ev' from type 'int' 

你可以有一個問題,malloc()realloc()沒有宣佈,也可能是未聲明CopyfromFile()。如果在使用函數之前沒有聲明函數,那麼C編譯器會假定它返回一個int - 這對於C89和預標準C是正確的。嚴格來說,對於C99或C11,編譯器應拒絕編譯,但大多數除非你告訴他們這樣做。

如果您使用GCC進行編譯,請使用嚴格的警告選項 - 尤其是要警告您丟失的原型。我使用:

gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \ 
    -Wold-style-definition -Werror … 

您可以使用-Wold-style-declaration過,或者相反,與海灣合作委員會的一些(舊的)版本可能不支持-Wold-style-*選項。如果你的代碼用這些選項乾淨地編譯,它可以避免很多問題。