title: how to create a forward declaration of a typedef struct
body: <p>I have 1 .h file <code>test.h</code> containing a class. In this class there's a private method returning a pointer on a type I don't want to make "public", but I want to be able to include this <code>test.h</code> file in other source files.</p>
  
  <p>In general, it's easy to use a forward declaration in the .h file:</p>
  
  <pre><code>class Foo;
  </code></pre>
  
  <p>But the problem is that this type comes from a C file <strong>that I cannot change</strong> (because it's someone else code that I'm not maintaining) and it's a <code>typedef</code>.</p>
  
  <p>So basically my <code>test.cpp</code> is:</p>
  
  <pre><code>// this type comes from a C file, cannot be changed to struct or class
  typedef struct 
  {
     int x;
  } Foo;
  
  #include "test.h"
  
  static Foo foo;
  
  Foo *Test::private_function()
  {
    foo.x = 12;
    return &amp;foo;
  }
  
  int Test::compute()
  {
     auto *local_foo = private_function();
     return local_foo-&gt;x;
  }
  </code></pre>
  
  <p>and my <code>test.h</code> file is:</p>
  
  <pre><code>#pragma once
  
  struct Foo;
  
  class Test
  {
  public:
    Test() {}
    int compute();
  private:
    Foo *private_function();
  };
  </code></pre>
  
  <p>trying to compile that fails:</p>
  
  <pre><code>&gt;g++ -std=c++11 -c test.cpp
  In file included from test.cpp:10:0:
  test.h:3:8: error: using typedef-name 'Foo' after 'struct'
  test.cpp:7:3: note: 'Foo' has a previous declaration here
  </code></pre>
  
  <p>Currently my workaround is to return <code>void *</code> and perform <code>static_cast</code> back and forth, but I don't find that optimal. Is there a nicer solution?</p>
  
  <p>(I have checked <a href="https://stackoverflow.com/questions/804894/forward-declaration-of-a-typedef-in-c">Forward declaration of a typedef in C++</a> but I tested the solutions and they don't seem to work in my case, maybe what I want to do is simpler/different - I have just a .h and .cpp - or just not possible)</p>