title: Passing objects into functions within another class c++
body: <p>I'm learning about classes and objects in c++ and tried the following code to test if I understood it properly:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class class1
  {
      public:
          void write(int x)
          {
               dataObject.var = x;
          }
  };
  
  class class2
  {
      public:
          void read()
          {
              std::cout &lt;&lt; dataObject.var;
          }
  };
  
  class data
  {
      public:
          int var;
          data()
          {
              var = 1;
          }
  };
  
  int main()
  {
      data dataObject;
      class1 object1;
      class2 object2;
  
  
      object2.read(data dataObject);
      object1.write(2);
      object2.read(data dataObject);
  
      return 0;
  }
  </code></pre>
  
  <p>This was for two objects to both be used to modify and use the members of a third but I get the following errors:</p>
  
  <pre><code>In member function 'void class1::write(int)':
  line 10: error: 'dataObject' was not declared in this scope
  
  In member function 'void class2::read()':
  line 14: error: 'dataObject' was not declared in this scope
  
  In function 'int main()':
  line 40 + 42: error: expected primary-expression before 'dataObject'
  </code></pre>
  
  <p>Any idea where I am going wrong?
  Thanks in advance.</p>
  
  <p>EDIT:
  Thanks for all the suggestions. My code now is:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class class1
  {
      public:
          void write(data &amp;dataObject, int x)
          {
              dataObject.var = x;
          }
  
  };
  
  class class2
  {
      public:
          void read(data dataObject)
          {
              std::cout &lt;&lt; dataObject.var &lt;&lt; endl;
          }
  };
  
  class data
  {
      public:
          int var;
          data()
          {
              var = 1;
          }
  };
  
  int main()
  {
      data dataObject;
      class1 object1;
      class2 object2;
  
  
      object2.read(dataObject);
      object1.write(dataObject,2);
      object2.read(dataObject);
  
      return 0;
  }    
  </code></pre>
  
  <p>And I now get the errors:</p>
  
  <p>8 error: 'data' has not been declared</p>
  
  <p>10 error: request for member 'var' in 'dataObject', which is of non-class type 'int'</p>
  
  <p>18 error: 'data' has not been declared</p>
  
  <p>20 error: request for member 'var' in 'dataObject', which is of non-class type 'int'</p>
  
  <p>40 error: 'dataObject' was not declared in this scope</p>