2

Possible Duplicate:
Forward declare FILE *

Suppose I want to write a wrapper class for C struct that is accest by a pointer to it such as FILE

in C you have to say

typedef struct _iobuf
    {
    /*content of this struct*/
    } FILE;

to get rid of the struct in object declarations. Now I want to do the following in filec.h:

class FILE;

class FileC
    {
    public:
        FileC(const char* name,const char* mode);
        ~FileC();
    private:
        FILE* fptr;
    };

And in filec.cpp:

#include "filec.h"
#include <cstdio>

//Implementation of FileC member functions

But when compiling I just get complaints about incomplete type struct FILE. Switching include order give that FILE already has been declared. I can solve it by using implementation specific details or use a void* instead FILE* casting back and forth. But how to do it nicely? Using iostream is not an answer because it also includes stdio anyway.

Community
  • 1
  • 1
user877329
  • 6,717
  • 8
  • 46
  • 88
  • FILE is a struct, but you forward declare as class. – nhahtdh May 29 '12 at 11:17
  • The forward declaration should be `struct FILE` why it is `class FILE`? – Naveen May 29 '12 at 11:17
  • Slight aside, you could use pimpl idiom. – hmjd May 29 '12 at 11:17
  • Maybe try `extern "C" struct FILE;`, to get the name mangling right. – Kerrek SB May 29 '12 at 11:19
  • 2
    @KerrekSB, no, type names are not mangled, `extern "C"` does nothing for types, only "function types, function names with external linkage, and variable names with external linkage have a _language linkage_" [dcl.link]/1 – Jonathan Wakely May 29 '12 at 11:25
  • 2
    @nhahtdh: If `FILE` were a `struct`, then it would be fine to declare it using `class`; the keywords are equivalent in declarations. However, it's not a class at all; it's a `typedef`, which can't be forward declared. – Mike Seymour May 29 '12 at 11:28
  • Use pimpl idiom to avoid using the C include in the client code – Valentin H Apr 19 '16 at 12:08

0 Answers0