C++ 不完整类型的无效使用';目录';

C++ 不完整类型的无效使用';目录';,c++,directory,dir,directory-listing,C++,Directory,Dir,Directory Listing,我正在尝试编译这段代码,它在Windows和Linux上运行良好(代码::块): /*编辑:包括*/ #包括 #包括 #包括 #包括 /**/ /* === */ /*功能代码*/ DIR*dp; dirent*ep; 字符串名称\u父项; dp=opendir(somepath); name\u parent=dp->dd\u name//错误 /**/ 由于Windows上的路径名不区分大小写,因此我可以读取用户输入,如“c://程序文件”,并获得“正确”路径“c:\program fil

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

/*编辑:包括*/
#包括
#包括
#包括
#包括
/**/
/* === */
/*功能代码*/
DIR*dp;
dirent*ep;
字符串名称\u父项;
dp=opendir(somepath);
name\u parent=dp->dd\u name//错误
/**/
由于Windows上的路径名不区分大小写,因此我可以读取用户输入,如“c://程序文件”,并获得“正确”路径“c:\program files*”(星号或“F://”->“F:*”除外)。我还使用这个变量来获取一个带有绝对路径值的目录列表,因为ep->d_name(当然是在某个readdir()之后)返回一个相对于某个路径的路径

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

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

我忘了什么吗? 还是存在逻辑错误


Edit:我已经在上面添加了include(我已经在使用了)。

显然,类型
DIR
在您尝试使用它时没有定义。可能您忘记了一个
#include

显然,类型
DIR
在您尝试使用它时没有定义。也许你忘了一个
#include

是的。您没有包含头文件

dirent.h

对。您没有包含头文件

dirent.h

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

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

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

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

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

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

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

用于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.

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.
/*编辑:包括*/
#包括
#包括
#包括
#包括
/**/
/* === */
/*功能代码*/
DIR*dp;
dirent*ep;
字符串名称\u父项;
dp=opendir(somepath);
ep=readdir(dp);
name\u parent=ep->d\u name;
/**/
变量d_name存在于提供目录名称的struct dirent中

/*Edit:Includes*/
#包括
#包括
#包括
#包括
/**/
/* === */
/*功能代码*/
DIR*dp;
dirent*ep;
字符串名称\u父项;
dp=opendir(somepath);
ep=readdir(dp);
name\u parent=ep->d\u name;
/**/

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

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

我的
文件.h

class Files
{
public:
    explicit Files(const char *p_path = 0);
    ~Files();

    /* ....  */
private:
    std::string path;
}
我的
文件.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;
    }
}
#包括
#包括
#包括
#包括
#include//我根据@Kerrek SB的建议添加了这一行,但没有任何更改
#包括
#包括
静态DIR*p_DIR=NULL;
静态结构目录*p_目录=NULL;
文件::文件(常量字符*p_路径)
{
if(p_path==NULL)
{

std::cerr我不会忘记包含一些标题或定义,现在我遇到了这个问题,但不是错误,而是警告

我的
文件.h

class Files
{
public:
    explicit Files(const char *p_path = 0);
    ~Files();

    /* ....  */
private:
    std::string path;
}
我的
文件.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;
    }
}
#包括
#包括
#包括
#包括
#include//我根据@Kerrek SB的建议添加了这一行,但没有任何更改
#包括
#包括
静态DIR*p_DIR=NULL;
静态结构目录*p_目录=NULL;
文件::文件(常量字符*p_路径)
{
if(p_path==NULL)
{

std::cerr抱歉-忘了提到它们(请参见编辑)。我已经在使用这些标题。它在Windows上工作。我想我很幸运有了代码::块?抱歉-忘了提到它们(请参见编辑)。我已经在使用这些标题。它在Windows上工作。我想我很幸运有了代码::块?