title: writing file in header c++
body: <p>I have 3 files, book.h, book.cpp, bookdriver.cpp. I want to know if the ISBNs in a list are found in the array using binary search.</p>
  
  <p>book.h:</p>
  
  <pre><code>using namespace std;
  ofstream fout("output2.txt");
  class Book
  {
      string author;          // A string for the name of the author
      string title;           //A string for the book title
      int ISBN;               //A long integer for the ISBN
  
      public:
          Book();                         //A default constructor
          void print();
          int getISBN() const;            //A const function GetISBN that returns the integer containing the ISBN.
          int binary_search(Book, int, int, int);
  };
  </code></pre>
  
  <p>book.cpp- also includes print function which uses fout </p>
  
  <pre><code>#include "book.h"
     //iterative binary search function, returns location of ISBN in array if present
  int Book::binary_search(Book arr[], int x, int n, int ISBN)
  {
      while (n &gt;= x)
      {
          int midpt = x + (n - x) / 2;
          //if ISBN is in midpoint
          if (arr[midpt].getISBN() == ISBN)
          {
              return midpt;
          }
          //if ISBN is greater than midpoint, ignore left side of array
          if (arr[midpt].getISBN() &lt; ISBN)
          {
              x = midpt + 1;
          }
          //if ISBN is smaller, ignore right side of array
          else
          {
              n = midpt - 1;
          }
      }
  
      //if ISBN not present
      return -1;
  }
  </code></pre>
  
  <p>bookdriver.cpp</p>
  
  <pre><code>#include "book.h"
  
  const int num = 10; //number of book objects the array should hold *can be changed*
  int main()
  {
      Book book_array[num] = {};  //array can hold num book objects
  
      for (int c = 0; c &lt; num; c++)
      {
          book_array[c] = Book();
          book_array[c].getData(data);        //reading book information
      }
  
      //read file
      ifstream fin("bookISBN.txt");
      int find_ISBN;
  
      while (fin &gt;&gt; find_ISBN)
      {
          bool match = false;
          int count = 0;
          int result = binary_search(book_array[10], 0, num - 1, find_ISBN); //error here
  
  
          if (result == -1)   //if ISBN not found
          {
              fout &lt;&lt; "Sorry, the ISBN " &lt;&lt; find_ISBN &lt;&lt; " is not found." &lt;&lt; endl;
          }
          else
          {
              fout &lt;&lt; "The ISBN " &lt;&lt; find_ISBN &lt;&lt; " is found in the system!" &lt;&lt; endl;
          }
          count++;
  
      }
      return 0;
  }
  </code></pre>
  
  <p>I'm using <code>fout</code> in both book.cpp and bookdriver.cpp so I have <code>ofstream fout (output2.txt)</code> in the header but I'm getting linker errors(Error LNK2005) in vs.</p>
  
  <p>I think because of the one definition rule, fout is defined twice?</p>