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

  • Thread starter Matthias =?ISO-8859-1?Q?K=E4ppler?=
  • Start date
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

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
 
G

Gianni Mariani

Matthias said:
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.

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

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

and
struct stat.
 
R

Rob Williscroft

Matthias Käppler wrote in in
comp.lang.c++:
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++.

#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.
 
V

Victor Bazarov

Matthias Käppler said:
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>.

How are they defined there? Neither header is standard, so its contenst
and the definitions of those structures are unknown in C++.
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++.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top