2016-04-07 21 views
0

Aşağıdaki komut satırı programını main.m'den main.mm'ye değiştirdiğimde tuhaf kod hataları alıyorum. Ana kadar iyi çalışıyor. Herkes nedenini biliyor mu? @johnelemans içinNeden Bu Hedef C/C++ Kodu main.m yerine main.m gerektiriyor?

https://stackoverflow.com/a/36469891/105539

KAYNAK

#import <Foundation/Foundation.h> 

void detectNewFile (
    ConstFSEventStreamRef streamRef, 
    void *clientCallBackInfo, 
    size_t numEvents, 
    void *eventPaths, 
    const FSEventStreamEventFlags eventFlags[], 
    const FSEventStreamEventId eventIds[]) 
{ 
    int i; 
    char **paths = eventPaths; 

    printf("GOT AN EVENT!!!!\n"); 
    for (i=0; i<numEvents; i++) { 
     printf("Change %llu in %s, flags %u\n", eventIds[i], paths[i], (unsigned int)eventFlags[i]); 
    } 
} 

int main(int argc, const char * argv[]) { 
    @autoreleasepool { 

     short nPathCount = 2; 
     CFStringRef mypath[nPathCount]; 
     mypath[0] = CFSTR("/Users/mike/Documents"); 
     mypath[1] = CFSTR("/Users/mike/Downloads"); 
     CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **)&mypath, nPathCount, NULL); 
     void *callbackInfo = NULL; 
     CFAbsoluteTime latency = 1.0; // seconds 

     FSEventStreamRef hStream = FSEventStreamCreate(NULL, 
      &detectNewFile, 
      callbackInfo, 
      pathsToWatch, 
      kFSEventStreamEventIdSinceNow, 
      latency, 
      kFSEventStreamCreateFlagFileEvents 
     ); 

     FSEventStreamScheduleWithRunLoop(hStream, CFRunLoopGetCurrent(),   kCFRunLoopDefaultMode); 
     FSEventStreamStart(hStream); 
     printf("Waiting on new file creations...\n"); 
     CFRunLoopRun(); // runs in an endless loop, only letting the callback function run 

    } // end autorelease pool 
    return 0; 
} 

HATALAR

FOR: 
     char **paths = eventPaths; 

Cannot initialize a variable of type 'char **' with an lvalue of type 'void *' 

FOR: 
     FSEventStreamRef hStream = FSEventStreamCreate(NULL, 
      &detectNewFile, 
      callbackInfo, 
      pathsToWatch, 
      kFSEventStreamEventIdSinceNow, 
      latency, 
      kFSEventStreamCreateFlagFileEvents 
     ); 

No matching function for call to 'FSEventStreamCreate' 
+0

Sorunuzu ilgili kod ve aldığınız hatalarla güncelleştirin. – rmaddy

+0

@rmaddy Okay, yaptım. – Volomike

+1

çünkü char ** yolları = eventPaths; (hatta Paralar bile geçersizdir) C cinsinden yasaldır, ancak C++ değildir. –

cevap

1

Teşekkür, ben sorun bulunamadı. C'de, void *'dan char **'a otomatik döküm yapmak yasaldır, ancak C++ uygulamasında bu değil. düzeltme casting kullanmaktır:

FSEventStreamContext *callbackInfo = NULL; 

... ve bunun yerine CFAbsoluteTime gibi değildi: FSEventStreamCreate üzerine, Sonra

char **paths = (char **)eventPaths; 

, bu * bunun yerine boşluğu gibi değildi Aşağıdakiler, yapı aşamalarına CoreServices.framework kitaplığı eklemeniz gerekir.

Bu değişiklikleri yaptım ve şimdi derliyor.

İlgili konular