C++ 编辑函数,return 向量

C++ edit function, return vector

请问如何修改vector<CContact>& addContact(const CContact &c)方法来传递这个assert?根据作业,函数应该 return a vector 是否正确?我正在添加简化的重要功能。

class CTimeStamp
{
  public:
    CTimeStamp(int year, int month, int day, int hour, int minute, int second);
...
};
        
CTimeStamp::CTimeStamp(int year, int month, int day, int hour, int minute, int second)
  : m_Year(year), m_Month(month), m_Day(day), m_Hour(hour), m_Minute(minute), m_Second(second)
{
}
        
class CContact
{
  public:
    CContact( CTimeStamp date, int numberFirst, int numberSecond );
        
  private:
    CTimeStamp m_Date;
    int m_NumberFirst, m_NumberSecond;
};
        
CContact::CContact( CTimeStamp date, int numberFirst, int numberSecond )
  : m_Date(date), m_NumberFirst(numberFirst), m_NumberSecond(numberSecond)
{
}
        
class CEFaceMask
{
  public:
    vector<CContact>& CEFaceMask::addContact(const CContact &c)
    {
      m_Db.push_back(c);
      return m_Db;
    }
  private:
    vector<CContact> m_Db;     
};
        
int main ()
{
  CEFaceMask test;
  test . addContact ( CContact ( CTimeStamp ( 2021, 1, 12, 12, 40, 10 ), 123456789, 111222333 ) )
       . addContact ( CContact ( CTimeStamp ( 2021, 2, 5, 15, 30, 28 ), 999888777, 555000222 ) );
}

按照您编写方法的方式,您不能将 addContact() 与另一个 addContact()(或任何其他 CEFaceMask 方法)链接起来。

或者改变你第二次调用addContact()的方式:

 test.addContact ( CContact ( CTimeStamp ( 2021, 1, 12, 12, 40, 10 ), 123456789, 111222333 ) );
 test.addContact ( CContact ( CTimeStamp ( 2021, 2, 5, 15, 30, 28 ), 999888777, 555000222 ) );

或更改 addContact() 的签名,以便它可以链式调用:

CEFaceMask& CEFaceMask::addContact(const CContact &c)
{
  m_Db.push_back(c);
  return *this;
}