title: C++ Linker Error. Undefined reference to Class::Function()
body: <p>I am new to C++ and I am creating a very simple program using classes. I am using Visual Studio Code to write my code and MinGW compiler to build and run it.
  I have written the following code as an example to show my problem.</p>
  <p>Main.cpp</p>
  <pre><code>#include &quot;class.h&quot;
  
  using namespace std;
  
  int main(void) {
      myClass myObject;
      myObject.sum(10, 20);
      return 0;
  }
  </code></pre>
  <p>class.h</p>
  <pre><code>#pragma once
  
  class myClass{
      public:
          void sum(int a, int b);
  };
  </code></pre>
  <p>class.cpp</p>
  <pre><code>#include &lt;iostream&gt;
  #include &quot;class.h&quot;
  
  using namespace std;
  
  void myClass::sum(int a, int b) {
      cout &lt;&lt; &quot;The sum = &quot; &lt;&lt; a + b &lt;&lt; endl;
  }
  </code></pre>
  <p>The compiler is giving me this error when I try to build it.</p>
  <blockquote>
  <p>undefined reference to `myClass::sum(int, int)'
  collect2.exe: error: ld returned 1 exit status</p>
  </blockquote>
  <p>BTW I know there are a lot of similar questions on the forum and I am sorry I am posting it again but all those solutions didn't seem to work for me as my program here is pretty simple. Any help will be very appreciated. Thanks!</p>