Home > Software design >  C edit function, return vector
C edit function, return vector

Time:03-13

May I ask how to modify the vector<CContact>& addContact(const CContact &c) method to pass this assert? Am I correct that the function should return a vector according to the assignment? I am adding reduced important functions.

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 ) );
}

CodePudding user response:

The way your methods are written, you cannot chain addContact() with another addContact() (or any other CEFaceMask method).

Either change the way you are calling addContact() the second time:

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

or change the signature of addContact() so that it can chain calls:

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