2009-08-09 9 views
0
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

static struct tm createDate(unsigned day, unsigned mon, int year) { 
     struct tm b = {0,0,0,day,mon-1,year-1900}; return b; 
} 

static int dateExceeded(unsigned day, unsigned mon, int year) { 
    struct tm b = createDate(day,mon,year); 
    time_t y = mktime(&b), now; 
    time(&now); // error C2143: syntax error : missing ';' before 'type' 
    double diff = difftime(y, now)/(60 * 60 * 24); // error C2065: 'diff' : undeclared identifier 
    return (diff < 0); 
} 

static void randomEvent(){ 
    srand(time(NULL)); 
    if (rand()%10) { 
      printf("Do something here\n"); // C2143: syntax error : missing ';' before 'type' 
    } 
} 
+0

Visual C++, 수집합니까? –

+0

은 서명이없는 유형입니까? – Zed

+0

@Matthew : 가장 가능성이 높습니다. 특히 GCC에서 잘 컴파일되기 때문에 가능합니다. – hbw

답변

5

C89 코드로 컴파일하는 경우 블록 시작 부분에 변수를 선언해야합니다. 당신은 블록의 중간에 double diff을 선언 할 수 없습니다 :

static int dateExceeded(unsigned day, unsigned mon, int year) { 
    double diff; 
    struct tm b = createDate(day,mon,year); 
    time_t y = mktime(&b), now; 
    time(&now); 
    diff = difftime(y, now)/(60 * 60 * 24); 
    return (diff < 0); 
} 
0

흠, 나는이 문제를 재현 할 수있을 것 같지 않습니다. 정확한 코드 사용 :

1>------ Build started: Project: so_1251288, Configuration: Debug Win32 ------ 
1>Compiling... 
1>so_1251288.cpp 
1>c:\users\matthew iselin\documents\visual studio 2008\projects\so_1251288\so_1251288\so_1251288.cpp(21) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data 
1>Linking... 
1>Build log was saved at *snip* 
1>so_1251288 - 0 error(s), 1 warning(s) 
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== 

저는 Visual C++를 사용한다고 가정했습니다. 어떤 버전을 사용하고 있습니까? 당신의 환경은 어떻게 구성되어 있습니까?

내가 생각할 수있는 유일한 점은 의도하지 않게 멀티 바이트 문자 인코딩이 아닌 유니 코드를 사용 가능하게했을 가능성이 있다는 것입니다. 그러나 오류가 발생해서는 안됩니다.

편집 : Visual C++ CLR 응용 프로그램을 만들고 직접 코드를 붙여 넣어도 재생할 수 없습니다. 문제를 진단하기 위해서는 좀 더 많은 정보가 필요합니다.

EDIT 2 : 실제로 C++ (/ TP) 코드 대신 C (/ TC)로 컴파일 할 때 재현 할 수 있습니다. 이미 언급했듯이 C89에서는 함수의 시작 부분에 변수를 정의해야하므로 코드가 실패하게됩니다.

+0

C 코드 (C++이 아님)로 컴파일하면 알게 될 것입니다. –

+0

그래, 나는 당신의 대답을 읽고 바로 그 테스트 :) –

0

코드에 실수가 있습니다. 프로그램의 일생에 한 번만 srand를 호출해야합니다. rand() 전에 항상 srand를 호출하면 동일한 값을 반복해서 가져올 수 있습니다.

0
ISO C90 forbids mixed declarations and code 
관련 문제