Search code examples
c++operator-overloadingassignment-operator

Assignment operator gives 'no suitable user-defined conversion' error


I have a CDateTime class with the following method:

CDateTime& operator=(const COleDateTime& datetime)
{
    SetDateTime(&datetime);
    return *this;
}

But it doesn't appear to be working.

enter image description here

Can someone tell me what I missed?


Solution

  • You are initializing, not assigning, dtStart and dtEnd variables. To make the initialization work, you need this constructor: CDateTime(const COleDateTime& datetime).

    Or, if CDateTime has default constructor, you can split the initialization into declaration and assignment:

    CDateTime dtStart; dtStart = dlg.m_dtStartDate;