How to pass an object of a class to a function of the same class?
<pre><code>int main(){

   int choice;
   fstream in, out;                             

  switch(choice)
  {

  case 1: 
      filename = s.studentRegister();
      out.open(filename.c_str(), ios::out);
      out.write((char *)&amp;s, sizeof(Student));
      cout&lt;&lt;endl&lt;&lt;"Registered successfully."&lt;&lt;endl;

  case 2:
      s.studentLogin();
  }                                   
}

class Student
{
std::string studentName, roll, studentPassword;
fstream in, out;

public:

std::string studentRegister()
{
    cout&lt;&lt;"Enter roll number"&lt;&lt;endl;
    cin&gt;&gt;roll;
    cout&lt;&lt;"Enter current semester"&lt;&lt;endl;
    cin&gt;&gt;ch;
    cout&lt;&lt;"Enter your name"&lt;&lt;endl;
    cin&gt;&gt;studentName;
    cout&lt;&lt;"Enter password"&lt;&lt;endl;
    cin&gt;&gt;studentPassword;

    return (roll+".dat");
}

void studentLogin()
{
            Student s;
    cout&lt;&lt;"Enter roll number: "&lt;&lt;endl;
            cin&gt;&gt;roll;
    cout&lt;&lt;"Enter password: "&lt;&lt;endl;
    cin&gt;&gt;studentPassword;

    filename = roll + ".dat";
    in.open(filename.c_str(), ios::in);
    in.read((char *)&amp;s, sizeof(Student));

    read.close();

    if((studentPassword.compare(s.studentPassword)==0))
    {
        system("cls");
        cout&lt;&lt;"Welcome "&lt;&lt;s.studentName&lt;&lt;"!"&lt;&lt;endl;
        displayStudentMenu();
    }

    else
    {
        cout&lt;&lt;"Invalid user";
        exit(0);
    }

}
</code></pre>

<p>I have two functions in my Student class: <code>studentRegister()</code> and <code>studentLogin()</code>. When studentRegister is invoked, it accepts all details of the student and then writes the object of the respective class to a DAT file. Now, when logging in, I'm trying to read the contents of the file into an object "s" using  <code>in.read((char *)&amp;s, sizeof(Student));</code></p>

<p>However, this leads to a run-time error and console closes abruptly. What is going wrong?</p>
