Do I need the C-style struct instantiation in C++ at all?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthias Käppler

    Do I need the C-style struct instantiation in C++ at all?

    Hi,

    I'm using a glibc system header <sys/stat.h> which defines a C struct
    'stat'. Furthermore, I'm using the struct 'dirent', defined in <dirent.h>.

    Now, at one point in my code I'm holding an std::list of dirents:

    std::list< dirent > mylist;

    This works.

    Now, I wanted to extend the list to hold pairs of dirent/stat:

    std::list< std::pair< dirent, stat > > mynewlist;

    Now, the compiler (g++ 3.3.4) says he doesn't know a type called 'stat'. So
    I tried the C-style notation for instantiating structs:

    std::list< std::pair< dirent, struct stat > > mynewlist;

    This works. I find it rather odd that I can pass 'dirent' without a
    preceding 'struct', but for 'stat' I have to do it the C way to calm the
    compiler.

    So, is there a rule of thumb in which cases I need the C-notation? Frankly I
    was quite sure that you don't need it at all in C++.

    Thanks in advance,
    Matthias
  • Gianni Mariani

    #2
    Re: Do I need the C-style struct instantiation in C++ at all?

    Matthias Käppler wrote:[color=blue]
    > Hi,
    >
    > I'm using a glibc system header <sys/stat.h> which defines a C struct
    > 'stat'. Furthermore, I'm using the struct 'dirent', defined in <dirent.h>.
    >
    > Now, at one point in my code I'm holding an std::list of dirents:
    >
    > std::list< dirent > mylist;
    >
    > This works.
    >
    > Now, I wanted to extend the list to hold pairs of dirent/stat:
    >
    > std::list< std::pair< dirent, stat > > mynewlist;
    >
    > Now, the compiler (g++ 3.3.4) says he doesn't know a type called 'stat'. So
    > I tried the C-style notation for instantiating structs:
    >
    > std::list< std::pair< dirent, struct stat > > mynewlist;
    >
    > This works. I find it rather odd that I can pass 'dirent' without a
    > preceding 'struct', but for 'stat' I have to do it the C way to calm the
    > compiler.[/color]

    In this case, there is a function called AND a struct called stat.

    i.e. func:
    stat(const char*, stat*)

    and
    struct stat.

    Comment

    • Rob Williscroft

      #3
      Re: Do I need the C-style struct instantiation in C++ at all?

      Matthias Käppler wrote in news:co7rn8$h18 $04$1@news.t-online.com in
      comp.lang.c++:
      [color=blue]
      > Hi,
      >
      > I'm using a glibc system header <sys/stat.h> which defines a C struct
      > 'stat'. Furthermore, I'm using the struct 'dirent', defined in
      > <dirent.h>.
      >
      > Now, at one point in my code I'm holding an std::list of dirents:
      >
      > std::list< dirent > mylist;
      >
      > This works.
      >
      > Now, I wanted to extend the list to hold pairs of dirent/stat:
      >
      > std::list< std::pair< dirent, stat > > mynewlist;
      >
      > Now, the compiler (g++ 3.3.4) says he doesn't know a type called
      > 'stat'. So I tried the C-style notation for instantiating structs:
      >
      > std::list< std::pair< dirent, struct stat > > mynewlist;
      >
      > This works. I find it rather odd that I can pass 'dirent' without a
      > preceding 'struct', but for 'stat' I have to do it the C way to calm
      > the compiler.
      >
      > So, is there a rule of thumb in which cases I need the C-notation?
      > Frankly I was quite sure that you don't need it at all in C++.
      >[/color]

      #include <list>
      #include <utility>

      struct dirent {};
      struct stat {};

      int stat; /* <- *note* */

      typedef std::list< std::pair< dirent, struct stat > > list_t;

      Remove the defenition "int stat;" and the "struct" should nolonger
      be needed.

      Its also possible that struct stat is undefined, in which case
      you should also get "struct stat is an incomplete type" errors
      or some such.

      HTH.

      Rob.
      --

      Comment

      • Victor Bazarov

        #4
        Re: Do I need the C-style struct instantiation in C++ at all?

        "Matthias Käppler" <nospam@digital raid.com> wrote...[color=blue]
        > I'm using a glibc system header <sys/stat.h> which defines a C struct
        > 'stat'. Furthermore, I'm using the struct 'dirent', defined in <dirent.h>.[/color]

        How are they defined there? Neither header is standard, so its contenst
        and the definitions of those structures are unknown in C++.
        [color=blue]
        > Now, at one point in my code I'm holding an std::list of dirents:
        >
        > std::list< dirent > mylist;
        >
        > This works.
        >
        > Now, I wanted to extend the list to hold pairs of dirent/stat:
        >
        > std::list< std::pair< dirent, stat > > mynewlist;
        >
        > Now, the compiler (g++ 3.3.4) says he doesn't know a type called 'stat'.
        > So
        > I tried the C-style notation for instantiating structs:
        >
        > std::list< std::pair< dirent, struct stat > > mynewlist;
        >
        > This works. I find it rather odd that I can pass 'dirent' without a
        > preceding 'struct', but for 'stat' I have to do it the C way to calm the
        > compiler.
        >
        > So, is there a rule of thumb in which cases I need the C-notation? Frankly
        > I
        > was quite sure that you don't need it at all in C++.[/color]

        That depends on how those things are defined. Another possibility is that
        there is something else in your program called 'stat' and by adding the
        keyword 'struct' you tell the compiler that you actually mean the type and
        not something else.

        Look in those headers, pull out the definitions for those structs into
        a separate file, see what the difference is between them.

        V


        Comment

        Working...