C++ friend class data access
<p>C++ newbie here.</p>

<p>I am a science guy writing a cfd (ish) code. I have created a class for all solving functions, and one that handles operations on a grid. The grid class wants to be able to see a few of the variables stored in the solving class, as passing them all to the grid class seems like a bit of effort.</p>

<p>So in my research I came across friend classes, but can't seem to get it to work. Please see the fully cut back example below. Class A is the solver, and it creates a grid class B. Even though I have written friend class B, I still get the following compile error (g++):</p>

<p>In member function 'void B::testB()':</p>

<p>error: 'a1' was not declared in this scope</p>

<p>Here is the code:</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;
class B {
private:
    int b1;

public:
    void testB(){
        cout&lt;&lt;a1&lt;&lt;endl;
    };  


};

class A {
friend class B;

private:
    int a1;

public:
    void testA(){
        a1=2;
        B b;
        b.testB();
        };
};


int main(){
    A a;
    a.testA();
}
</code></pre>
