开发者

Invalid use of incomplete type 'DIR'

开发者 https://www.devze.com 2023-03-25 09:15 出处:网络
I\'m trying to compile this code, which works fine on Windows, on Linux (Code::Blocks): /* Edit: Includes */

I'm trying to compile this code, which works fine on Windows, on Linux (Code::Blocks):

/* 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
/**/

Since path names on Windows are not case sensitive, I can read a user input like "c://program files" and get the "correct" path "C:\Program Files*" (except for the asterisk - or "F://" -> "F:*"). I also use this variable 开发者_如何学Pythonto get a directory listing with absolute path values, since ep->d_name (after some readdir() of course) returns a path relative to somepath.

On Linux, I get a compiler error (for "dp->dd_name"):

error: invalid use of incomplete type 'DIR'

Did I forget something? Or is there a logical error?

Edit: I've added the includes (that I'm already using) above.


/* 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;

/**/

The variable d_name exists in the struct dirent which gives the name of the directory


You didn't declare the type of DIR! On Posix systems, you would have said,

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

However, on Windows, you don't have these features. Instead, you could use the Windows API filesystem functions.


yes. you missed including header files.

dirent.h


The internal structure of a DIR is unspecified, so you should never rely on it and expect your code to be portable.

The glib source for Windows says this about DIR:

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


Apparently, the type DIR is not defined at the point you're trying to use it. Maybe you forgot an #include?


It is not about to forget including some headers or definition now I've faced this problem but not error it was warning.

My files.h;

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

    /* ....  */
private:
    std::string path;
}

My 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;
    }
}

The warning at the definition line (static DIR *p_dir = NULL;) is 'p_dir' has incomplete type and the warning at the delete line (delete p_dir;) is possible problem detected in invocation of delete operator: [-Wdelete-incomplete].

After changing the delete p_dir; with free(p_dir); the both warnings are gone. I don't know it's exact reason but it sounds like DIR * type acting like void *. I'm just taking a wild guess.

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消