title: How to initialise and return an array in a c++ header file?
body: <p>I'm not very good with header files but I want to use a header file to read data from a file and return the data as a vector in the main cpp file.</p>
  
  <p>Here is my readposcar.h file:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  int add(void) {
  
      double a1x, a1y, a1z, a2x, a2y, a2z, a3x, a3y, a3z; // I want all this stuff in vector form
      int i;
      double scale;
      string line; stringstream dum;
  
      ifstream poscar ("POSCAR");
      for (i=1; i&lt;=5; i++) {
          getline(poscar,line);
          if (i==2) {stringstream dum(line); dum &gt;&gt; scale;}
          if (i==3) {stringstream dum(line); dum &gt;&gt; a1x &gt;&gt; a1y &gt;&gt; a1z;}
          if (i==4) {stringstream dum(line); dum &gt;&gt; a2x &gt;&gt; a2y &gt;&gt; a2z;}
          if (i==5) {stringstream dum(line); dum &gt;&gt; a3x &gt;&gt; a3y &gt;&gt; a3z;}
      }
  
      vector&lt;double&gt; myvec(3);
      myvec[0] = a1x;
      myvec[1] = a1y;
      myvec[2] = a1z;
      return myvec;
  }
  </code></pre>
  
  <p>Here is my .cpp file:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  #include "readposcar.h"
  
  using namespace std;
  
  int main(void) {
      int nbasis = 2;
      int nkpts = 10;
      vector&lt;double&gt; myvec2(3);
      myvec2 = add();
      cout &lt;&lt; "No. of k-points: " &lt;&lt; nkpts &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>This obviously does not work. Can someone please advise on what's wrong and what I need to do to make it work? I can only get it to work if I do return say myvec[2] in the .h file but not the entire array. </p>
  
  <p>I don't mind having it as an array if vectors don't work. Is it perhaps possible to just initialise the array in the header file as a sort of global array and then simply call it in the .cpp file?</p>
  
  <p>Here are the errors I get:</p>
  
  <p>In file included from main.cpp:4:0:</p>
  
  <pre><code>readposcar.h: In function ‘int add()’:
  readposcar.h:27:9: error: cannot convert ‘std::vector&lt;double&gt;’ to ‘int’ in return
  main.cpp: In function ‘int main()’:
  main.cpp:12:15: error: no match for ‘operator=’ in ‘myvec2 = add()’
  </code></pre>