title: Objects and functions in C++
body: <p>I'm working on something that will take in an input file of strings and organize them based on a user input file. The top of the input file is the way it's organized and the rest is formatted information, each on it's own line to take in to feed into an object. <em>Each individual piece of information that's needed for my object is divided into 5 consecutive lines.</em></p>
  
  <p>My problem is the print function, which would be called on the array of object pointers and pass in the user sort preference when called. What I've found so far is that I am unable to to even call the function on the object. I've tried a bunch of different solutions like creating an entirely new object (Object myObject();) and calling the same print function on it but that won't work either. Is there a way to call the function on the array of pointers to objects that I created and pass in that preference variable? <strong>I've tried adding a default constructor and calling the function on that, but when I did it the two tests in object.cpp that cout the variables when print is called show that the variables are passed in as NULL possibly.</strong></p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "object.h"
  
  int main()
  {
      Object **ptr = new Object*[5];//Create array of object pointers
  
      string firstLine, secondLine, thirdLine, userPreference;
      float fourthLine;
      int fifthLine;
  
      string currentLine;
      int lineCounter = 0;
      int arrayCounter = 0;
  
      getline(cin, userPreference);//Preference on sorting method
  
      while(getline(cin, currentLine)) {//Main loop
          switch(lineCounter) {
              case(1):
                  firstLine = currentLine;
                  lineCounter++;
              break;
  
              case(2):
                  secondLine = currentLine;
                  lineCounter++;
              break;
  
              case(3):
                  thirdLine = currentLine;
                  lineCounter++;
              break;
  
              case(4):
                  fourthLine = stof(currentLine);//Get float value of currentLine and set fourthLine equal to it
                  lineCounter++;
              break;
  
              case(5):
                  fifthLine = stoi(currentLine);//Get integer value of currentLine and set fithLine equal to it
                  ptr[arrayCounter] = new Object(firstLine, secondLine, thirdLine, fourthLine, fifthLine);
                  arrayCounter++;//Step to next element now that the current element is filled
                  lineCounter = 0;//Reset lineCounter
                  cout &lt;&lt; "case 5 test" &lt;&lt; endl;
              break;
          }//End switch
      }
  
      ptr -&gt; print(userPreference);
  
      for(int i = 0; i &lt; arrayCounter; i++) {//Delete everything
          cout &lt;&lt; "Test";
      }
  }
  </code></pre>
  
  <p>object.cpp</p>
  
  <pre><code>#include "object.h"
  #include &lt;iostream&gt;
  
  using namespace std;
  
  Object::Object(string title, string URL, string comment, float length, int rating) {
      vidTitle = title;
      vidURL = URL;
      vidComment = comment;
      vidLength = length;
      vidRating = rating;
  }
  
  void Object::print(string preference) {
      vidPreference = preference;
      cout &lt;&lt; vidPreference;
      cout &lt;&lt; preference;
      if(vidPreference == "rating") {
          //Rating sort
          //Is the array passed through correctly? If so, can I climb through like I do to assign it in main?
      }
  
      if(vidPreference == "length") {
          //Length sort
      }
  
      if(vidPreference == "title") {
          //Title sort
      }
      else {
          cout &lt;&lt; preference &lt;&lt; " is not a legal sorting method, giving up." &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>object.h</p>
  
  <pre><code>#ifndef OBJECT_H
  #define OBJECT_H
  
  #include &lt;string&gt;
  
  using namespace std;
  
  class Object
  {
  
      public:
          Object(string title, string URL, string comment, float length, int rating);
          void print(string preference);
      private:
          string vidTitle, vidURL, vidComment, vidPreference;
          float vidLength;
          int vidRating;
  };
  
  #endif
  </code></pre>