Search code examples
c++return

What type does a function return when you just use return; in c++


I use a function to return a obj I found in the data file. I wonder what type of data it returns when there is no corresponding obj. This is my codes.

BankAccount Find(string account_num , string password )
{
  FILE_IN.seekg(0 , ios::beg);
  int check = 0;
  string temp1;
  string temp2;
  string temp3;
  while(!FILE_IN.eof())
  {
    getline(FILE_IN , temp1);
    getline(FILE_IN , temp2);
    getline(FILE_IN , temp3);
    if(account_num.compare(temp1) == 0 && password.compare(temp2) == 0)
    check = 1;
  }
  if(check == 1)
  {
    fstream file;
    file.open(temp3 , ios::out);
    string name;
    string hist;
    string bal;
    getline(file , name);
    getline(file , bal);
    file.ignore(numeric_limits<streamsize>::max() , '\n');
    getline(file , hist , 'H');
    BankAccount account = BankAccount(atoi(account_num.c_str()) , atoi(bal.c_str()) , password , name );
    account.Enter_History(hist);
    return account;
  }
  else
  {
    return;
  }
}

Solution

  • Your program is ill-formed and should not compile without error. Using a return statement without operand/value in a function that has a non-void return type is not allowed, see the C++17 standard draft in [stmt.return]/2.