【问题标题】:Invalid use of incomplete type 'DIR'不完整类型“DIR”的使用无效
【发布时间】:2011-07-31 11:52:16
【问题描述】:

我正在尝试编译这段代码,它在 Windows 上运行良好,在 Linux 上运行良好(代码::块):

/* Edit: Includes */
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <...>
/**/

/* === */

/* Function code */
DIR *dp;
dirent *ep;
string name_parent;

dp = opendir(somepath);
name_parent = dp->dd_name; //error
/**/

由于 Windows 上的路径名不区分大小写,我可以读取“c://program files”之类的用户输入并获取“正确”路径“C:\Program Files*”(星号除外 - 或"F://" -> "F:*")。我还使用此变量来获取具有绝对路径值的目录列表,因为 ep->d_name(当然是在一些 readdir() 之后)返回相对于 somepath 的路径。

在 Linux 上,我收到编译器错误(对于“dp->dd_name”):

错误:不完整类型“DIR”的使用无效

我是不是忘记了什么? 还是有逻辑错误?

编辑:我在上面添加了包含(我已经在使用)。

【问题讨论】:

    标签: c++ directory dir directory-listing


    【解决方案1】:
    /* Edit: Includes */
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    #include <...>
    /**/
    
    /* === */
    
    /* Function code */
    DIR *dp;
    dirent *ep;
    string name_parent;
    
    dp = opendir(somepath);
    ep = readdir(dp);
    name_parent = ep->d_name;
    
    /**/
    

    变量 d_name 存在于 struct dirent 中,它给出了目录的名称

    【讨论】:

      【解决方案2】:

      你没有声明DIR的类型!在 Posix 系统上,你会说,

      #include <sys/types.h>
      #include <dirent.h>
      

      但是,在 Windows 上,您没有这些功能。相反,您可以使用Windows API filesystem functions

      【讨论】:

      • 对不起 - 忘了提到他们(见编辑)。我已经在使用这些标题了。它在 Windows 上运行。我想我对 Code::Blocks 很幸运?
      【解决方案3】:

      是的。你错过了包括头文件。

      dirent.h
      

      【讨论】:

        【解决方案4】:

        DIR 的内部结构未指定,因此您永远不应依赖它并期望您的代码是可移植的。

        Windows 的 glib 源代码提到了 DIR

        /*
         * This is an internal data structure. Good programmers will not use it
         * except as an argument to one of the functions below.
        

        【讨论】:

          【解决方案5】:

          显然,DIR 类型在您尝试使用它时并未定义。也许你忘记了#include

          【讨论】:

            【解决方案6】:

            现在我已经遇到了这个问题,但不会忘记包含一些标题或定义,但它不是警告的错误。

            我的files.h;

            class Files
            {
            public:
                explicit Files(const char *p_path = 0);
                ~Files();
            
                /* ....  */
            private:
                std::string path;
            }
            

            我的files.cpp;

            #include <iostream>
            
            #include <stdio.h>
            #include <stdlib.h>
            #include <stdarg.h>
            #include <sys/types.h> // I added this line with @Kerrek SB's advice but nothing changed
            #include <dirent.h>
            #include <files.h>
            
            static DIR *p_dir = NULL;
            static struct dirent *p_ent = NULL;
            Files::Files(const char *p_path)
            {
                if (p_path == NULL)
                {
                    std::cerr << "PATH is NULL" << std::endl;
                    exit(EXIT_FAILURE);
                }
                path = p_path;
                p_dir = opendir(p_path);
                if (p_dir == NULL)
                {
                    std::cerr << "Cannot open " << path << std::endl;
                    exit(EXIT_FAILURE);
                }
            }
            
            Files::~Files()
            {
                if (p_dir)
                {
                    /* Here is my warning occuring in this line and the definition
                       line p_dir 'static DIR *p_dir = NULL' */
                    delete p_dir; // After changing this line with 'free(p_dir);' warnings gone.
                    p_dir = NULL;
                }
            }
            

            定义行(static DIR *p_dir = NULL;)的警告是'p_dir' has incomplete type,删除行(delete p_dir;)的警告是possible problem detected in invocation of delete operator: [-Wdelete-incomplete]

            delete p_dir; 更改为free(p_dir); 后,两个警告都消失了。 我不知道它的确切原因,但它听起来像DIR * 类型的行为就像void *。我只是在胡乱猜测。

            希望这会有所帮助。

            【讨论】:

              猜你喜欢
              • 2017-10-24
              • 2010-10-13
              • 2013-05-19
              • 2016-04-06
              • 2017-07-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多