title: C++: "Field 'object_var' must be initialized" in constructor of Base class
body: <p>I am programming a polymorphic program in C++ that requires the use of Derived/Inherited classes. My base class is called <code>ClassA</code> and my Derived class is called <code>ClassB</code>. I have a separate class called <code>varClass</code> that is the datatype for a parameter in the constructor of my Base class, <code>ClassA</code>. I get this error message in the following constructor in <code>ClassA.cpp</code>:</p>
  
  <blockquote>
    <p>Field 'object_var' must be initialized</p>
  </blockquote>
  
  <pre><code>#include "ClassA.h"
  #include "varClass.h"
  
  ClassA::ClassA(const classVar &amp;O, const int &amp;p) {}
  </code></pre>
  
  <p>Why must objects from a separate concrete class be initialized before allowing them to be used a parameters of functions in separate classes? I have tried putting a forward declaration of the <code>classVar</code> class in <code>ClassA</code> with no luck. How can I fix this to allow an argument of a datatype from a separate class? If I give it an initialization, how can I ensure each value passed to this constructor is unique and not just overridden by the initialization value?</p>
  
  <p>Here is the rest of the relevant code. I am not going to show the Derived classes as I do not believe they are part of the problem, but decided to mention them since I am a novice programmer and that could possibly be the issue. </p>
  
  <pre><code>// ClassA.h
  
  #ifndef CLASSA_H
  #define CLASSA_H
  
  #include "varClass.h"
  
  class ClassA {
  
  protected:
      const classVar &amp;object_var; // The problem lies here
      unsigned var1;
  
   // ... more code follows
  
  public:
      ClassA(const object &amp;O, const int &amp;p);
  
  // ... more code follows
  
  };
  
  #endif 
  </code></pre>
  
  <p>Header and implementation of <code>objectVar</code> class are irrelevant to issue. That class is a standard concrete class.</p>