title: Accessing vectors declared in header file C++ (not global)
body: <p>I've submitted an assignment which has met all the criteria except that I'm not allowed to have global variables except if they're constants, and I had my vector set up as a global variable.</p>
  <p>I'm now finding it impossible to have my vector used in my header file as well as my class functions file and my main class file.</p>
  <p>I have a primary class and a derived class, and the vector items are of the derived class. I've put my vector definition in the header now in the primary class:</p>
  <pre><code>vector&lt;Stock&gt; arrStock;
  </code></pre>
  <p>But when trying to add objects into the vector from the input file and subsequently perform actions on it, I am able to either set up the functions inside the classes functions class, OR call the functions I've set up in the main class (if I have the functions set up as static).</p>
  <p>What am I doing wrong? Currently with the way the code has been set up, I'm having error E0245 &quot;a nonstatic member reference must be relative to a specific object&quot; on all the functions that I'm calling in my main class. It's probably worth noting that when I had the vector set up as a global variable, the functions were defined as static and they worked perfectly, so I'm open to the fact that they need to be static and that my issue is with the application of the array in the Assignment2_classes.cpp file instead.</p>
  <p>Please see my code snippets below:</p>
  <p>Assignment2.h:</p>
  <pre><code>class Product 
  {
  
  private:
      string  title, surname;
      long long int isbn;
      double  wholesalePrice;
  
  public:
      string getTitle();
      string getSurname();
      long long int getIsbn();
      double getWholesalePrice();
  
      void setTitle(string);
      void setSurname(string);
      void setIsbn(long long int);
      void setWholesalePrice(double);
      
      Product();
      ~Product();
      Product(string, string, long long int, double);
  
      void importProducts();
      void newProduct();
      void delProduct();
      void runReport();
      void checkStock();
      void clrStock();
  
      vector&lt;Stock&gt; arrStock;
  };
  
  // creating the derived class Stock
  class Stock :public Product 
  {
  
  public:
      double retailPrice;
      char bookFormat;
      int stockLevel;
  
      double getRetailPrice();
      char getBookFormat();
      int getStockLevel();
      
      void setRetailPrice(double);
      void setBookFormat(char);
      void setStockLevel(int);
  
      Stock();
      ~Stock();
      Stock(string, int, char, string, double, long long int, double);
      
      void setStockInfo(long long int, string, string, double, int, double, char);
      
  };
  </code></pre>
  <p>Assignment2_classes.cpp:</p>
  <pre><code>void Product::importProducts()
  {
      // code adapted from: https://stackoverflow.com/questions/16878259/how-to-read-in-a-set-of-values-from-a-text-file-then-go-to-the-next-line-and-do
      // telling the function which input file it is reading from
      ifstream productsFile(&quot;products_v5.txt&quot;);
      
      // creating local variables
      Stock aStock;
      double tempRetailPrice = 0;
      string undsc = &quot;_&quot;;
      string space = &quot; &quot;;
      size_t position;
  
  
      std::cout &lt;&lt; &quot;Importing books...\n&quot;;
      
      // reading the books into an array
      while (productsFile &gt;&gt; aStock.title &gt;&gt; aStock.stockLevel &gt;&gt; aStock.bookFormat &gt;&gt; aStock.surname &gt;&gt; aStock.wholesalePrice &gt;&gt; aStock.isbn)
      {
          // replacing the underscores in the title names with spaces so the output looks better
          // code adapted from https://www.educba.com/c-plus-plus-replace/
          while ((position = aStock.title.find(undsc)) != string::npos)
          {
              aStock.title.replace(position, 1, space);
          }
          
          // calculating the retail prices of the books depending on their format
          switch (aStock.bookFormat)
          {
          case 'a': tempRetailPrice = aStock.wholesalePrice * 1.43;
              break;
          case 'e': tempRetailPrice = aStock.wholesalePrice * 1.08;
              break;
          case 'h': tempRetailPrice = aStock.wholesalePrice * 1.45;
              break;
          case 's': tempRetailPrice = aStock.wholesalePrice * 1.27;
              break;
          }
          aStock.setRetailPrice(tempRetailPrice);
          arrStock.push_back(aStock);
      }
  
      // letting the user know how many books have been added and how many books are currently in the array
      std::cout &lt;&lt; &quot;\n&quot; &lt;&lt; to_string(arrStock.size()) &lt;&lt; &quot; books have been added.\n&quot;;
      std::cout &lt;&lt; &quot;\nBiblioden Books currently has &quot; &lt;&lt; to_string(arrStock.size()) &lt;&lt; &quot; different books.\n&quot;;
  }
  </code></pre>
  <p>Assignment2_main.cpp:</p>
  <pre><code>int main()
  {
  
      char createBook;
      char deleteBook;
      char viewReport;
      char checkOrders;
  
      // creating the heading of the output
      cout &lt;&lt; &quot;-----------------------------------------------------------------------------------------\n&quot; &lt;&lt; &quot;  Biblioden Systems\n&quot; &lt;&lt; &quot;-----------------------------------------------------------------------------------------\n&quot;;
      
      ifstream productsFile(&quot;products_v5.txt&quot;);
      
      // checking whether the file is open and gracefully exiting if it can't be opened
      if (!productsFile.is_open())
      {
          cout &lt;&lt; &quot;\nCannot open file.\n&quot;;
          return 1;
      }
      Product::importProducts();
      
      //closing the file
      productsFile.close();
      
      cout &lt;&lt; &quot;\nWould you like to enter a new book? (Y/N): &quot;;
      cin &gt;&gt; createBook;
      if (createBook == 'Y' || createBook == 'y')
      {
          Product::newProduct();
      }
  
      cout &lt;&lt; &quot;\nWould you like to delete a book? (Y/N) &quot;;
      cin &gt;&gt; deleteBook;
  
      if (deleteBook == 'Y' || deleteBook == 'y')
      {
          Product::delProduct();
      }
  
      cout &lt;&lt; &quot;\nWould you like to view a report? (Y/N) &quot;;
      cin &gt;&gt; viewReport;
      if (viewReport == 'Y' || viewReport == 'y')
      {
          ofstream report(&quot;report.txt&quot;);
  
          // checking whether the file is open and gracefully exiting if it can't be opened
          if (!report.is_open())
          {
              cout &lt;&lt; &quot;\nCannot open file.\n&quot;;
              return 1;
          }
          else
          {
              Product::runReport();
              
              // closing the file
              report.close();
          }
      }
  
      cout &lt;&lt; &quot;\nWould you like to check the order list against the stock list? (Y/N) &quot;;
      cin &gt;&gt; checkOrders;
      if (checkOrders == 'Y' || checkOrders == 'y')
      {
          ifstream ordersFile(&quot;orders_v5.txt&quot;);
  
          // checking whether the file is open and gracefully exiting if it can't be opened
          if (!ordersFile.is_open())
          {
              cout &lt;&lt; &quot;\nCannot open file.\n&quot;;
              return 1;
          }
          else
          {
              Product::checkStock();
              // closing the file
              ordersFile.close();
          }
      }
  
      // clearing out the array once the user is finished with it
      Product::clrStock();
      return 0;
  }
  </code></pre>