title: C++: passing the this pointer to another class
body: <p>I'm new to C++ but I do have some experience in Java.
  While coding, I stumbled across an error that confused me.
  Here's my code (simplified, but the errors are the same):</p>
  
  <p><strong>A.h:</strong></p>
  
  <pre><code>#pragma once
  #include "B.h"
  class A
  {
  public:
      A();
      void foo();
      void sayHello();
      B b;
   };
  </code></pre>
  
  <p><strong>A.cpp:</strong></p>
  
  <pre><code>#include "A.h"
  #include &lt;iostream&gt;    
  A::A() {}
  void A::foo() {
      b.bar(this);
  }
  void A::sayHello() {
      std::cout &lt;&lt; "Hello" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p><strong>B.h:</strong></p>
  
  <pre><code>#pragma once
  #include "A.h"
  class B
  {
  public:
      B();
      void bar(A *a);
  };
  </code></pre>
  
  <p><strong>B.cpp:</strong></p>
  
  <pre><code>#include "B.h"
  B::B(){}
  void B::bar(A *a) {
      a-&gt;sayHello();
  }
  </code></pre>
  
  <p>I want to pass a pointer of the <em>a</em> object to the <em>bar</em> function in <em>B</em>, so that I'll be able to modify and access <em>a</em>'s fields in <em>bar</em>. Strangely, I get these errors when I call <em>foo</em> through an instance of <em>A</em> from another class:</p>
  
  <pre><code>1&gt;------ Build started: Project: Test, Configuration: Debug Win32 ------
  1&gt;  main.cpp
  1&gt;d:\stuff\visual studio 2015\projects\test\test\b.h(7): error C2061: syntax error: identifier 'A'
  1&gt;  B.cpp
  1&gt;d:\stuff\visual studio 2015\projects\test\test\a.h(9): error C3646: 'b': unknown override specifier
  1&gt;d:\stuff\visual studio 2015\projects\test\test\a.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;  A.cpp
  1&gt;d:\stuff\visual studio 2015\projects\test\test\b.h(7): error C2061: syntax error: identifier 'A'
  1&gt;d:\stuff\visual studio 2015\projects\test\test\a.cpp(5): error C2660: 'B::bar': function does not take 1 arguments
  1&gt;  Generating Code...
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  
  <p>The code works fine if I don't include <em>A.h</em> in <em>B.h</em> and I don't pass anything to the <em>bar</em> function.</p>
  
  <p>I tried to google what could cause these errors but I wasn't able to resolve the issue by myself becuase I don't understand what's causing these errors. What am I doing wrong?</p>