结构DIR的前向声明

时间:2011-12-08 15:14:11

标签: c++ c include

我以某种方式弄乱了includes,但无法真正找到错误:

#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

编译器输出:

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’

€:我很抱歉给您带来不便,但在我开始切换线路之前,我粘贴了一个旧的错误代码,但现在它是正确的。

3 个答案:

答案 0 :(得分:3)

curr_dir->d_name应为elem->d_name

如错误所示,您错误地尝试取消引用指向不透明类型DIR的指针。

答案 1 :(得分:3)

DIR *是一个不透明的句柄,你无法访问它的内部。您可能打算访问elem而不是curr_dir

即。改变

form_uri(start,curr_dir->d_name);

form_uri(start,elem->d_name);

答案 2 :(得分:0)

dirent.h says:
/* This is the data type of directory stream objects.
   The actual structure is opaque to users.  */
typedef struct __dirstream DIR;