简体   繁体   中英

forward declaration of struct DIR

I somehow messed up the includes , but weren't able to actually find the error:

#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifdef DEBUG
#define DLOG(x) printf(x)
#define PLOG(x,y) printf(x,y)
#else
#define DLOG(x)
#define PLOG(x,y)
#endif

harddrive::Results* harddrive::search_for(char* start,char* target,char** ignore,int size) {
PLOG("work directory: %s",start);
DIR* curr_dir = opendir(start);
Results* local = new Results;

if(!curr_dir) {
    printf(" opendir() failure, probably no real directory: %s",start);
    errno = 0;
    return NULL;
}

struct dirent* elem;
while( (elem = readdir(curr_dir)) ) {
    //form URI
    char* uri = form_uri(start,curr_dir->d_name); //here is the actual error
    struct stat st;
    lstat(elem->d_name,&st);
    if( S_ISDIR(st.st_mode) ) {
        if( !do_ignore(uri,ignore,size) )
            local = merge(local,search_for( form_uri(start,elem->d_name), target,ignore,size));
    } 
    else if( S_ISREG(st.st_mode) ) { //this is line 41

Compiler output:

Directory.cpp: In function ‘harddrive::Results* harddrive::search_for(char*, char*, char**, int)’:
Directory.cpp:34:38: error: invalid use of incomplete type ‘struct DIR’
/usr/include/dirent.h:128:16: error: forward declaration of ‘struct DIR’

€:I am sorry for the inconvenience but i pasted an old error code, before I started switching around lines, but now it is correct.

curr_dir->d_name should be elem->d_name .

As the error says, you're incorrectly trying to dereference a pointer to the opaque type DIR .

DIR* is an opaque handle, you can't access its internals. You probably meant to access elem instead of curr_dir

ie change

form_uri(start,curr_dir->d_name);

to

form_uri(start,elem->d_name);
dirent.h says:
/* This is the data type of directory stream objects.
   The actual structure is opaque to users.  */
typedef struct __dirstream DIR;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM