Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pass by reference error

pass by reference error

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    minkowski
    wrote on last edited by
    #1

    Hi I am trying to pass by reference a variable. Below is the code. In the class def I have void OnLoseFocusDate( Date &jdtDate); and in the src file I have void CEQSLowerInterestLeg::OnLoseFocusDate( Date &jdtDate) { if( jdtDate != Util::grdtNull) { Date adjust_date( jdtDate ); const CCalendar *calendar = CHolidayMgr::GetInstance().GetEquityLegPaymentHoliday(); if (calendar) { adjust_date = calendar->BusinessDayIf(adjust_date, (EnBusDayConvention)m_jcmBDC.GetSelectedObjectID(), &ToolUtils::PromptForBusinessDay); jdtDate = adjust_date; } } } // end of OnLoseFocusDate with the function call being OnLoseFocusDate(m_jdtTerminationDate.GetValue()); where GetValue() is defined as Date NewDate::GetValue(void) { if (::IsWindow(this->m_hWnd) && GetFocus() == this ) { if( m_dateFormat == shortFormat ) { SetError(!IsValid()); } } return m_value; } and m_value is defined as Date m_value; The error I am getting is error C2220: warning treated as error - no object file generated C:\viewstr\ptp6970-DateChange\Swaps\ptp\src\eqslowerinterestleg.cpp(2039) : warning C4239: nonstandard extension used : 'argument' : conversion from 'class Date' to 'class Date &' A reference that is not to 'const' cannot be bound to a non-lvalue I dont understand why I get this error and I dont believe the solution is to pass in by const reference since I modify the argument in the function. Can someone please explain why I get this error and a solution? I have found that if I do Date xx = m_jdtTerminationDate.GetValue(); OnLoseFocusDate(xx); then it is ok but I dont understand the difference between the 2 methods. Many thanks !

    C B 2 Replies Last reply
    0
    • M minkowski

      Hi I am trying to pass by reference a variable. Below is the code. In the class def I have void OnLoseFocusDate( Date &jdtDate); and in the src file I have void CEQSLowerInterestLeg::OnLoseFocusDate( Date &jdtDate) { if( jdtDate != Util::grdtNull) { Date adjust_date( jdtDate ); const CCalendar *calendar = CHolidayMgr::GetInstance().GetEquityLegPaymentHoliday(); if (calendar) { adjust_date = calendar->BusinessDayIf(adjust_date, (EnBusDayConvention)m_jcmBDC.GetSelectedObjectID(), &ToolUtils::PromptForBusinessDay); jdtDate = adjust_date; } } } // end of OnLoseFocusDate with the function call being OnLoseFocusDate(m_jdtTerminationDate.GetValue()); where GetValue() is defined as Date NewDate::GetValue(void) { if (::IsWindow(this->m_hWnd) && GetFocus() == this ) { if( m_dateFormat == shortFormat ) { SetError(!IsValid()); } } return m_value; } and m_value is defined as Date m_value; The error I am getting is error C2220: warning treated as error - no object file generated C:\viewstr\ptp6970-DateChange\Swaps\ptp\src\eqslowerinterestleg.cpp(2039) : warning C4239: nonstandard extension used : 'argument' : conversion from 'class Date' to 'class Date &' A reference that is not to 'const' cannot be bound to a non-lvalue I dont understand why I get this error and I dont believe the solution is to pass in by const reference since I modify the argument in the function. Can someone please explain why I get this error and a solution? I have found that if I do Date xx = m_jdtTerminationDate.GetValue(); OnLoseFocusDate(xx); then it is ok but I dont understand the difference between the 2 methods. Many thanks !

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      The problem is that when you return something from a function (for example your GetValue function), a copy of your object will be made (you don't return the object itself but a copy). So in your case, passing it by reference would mean that the function (OnLoseFocusDate) will probably want to modify it. But in your case it is totally useless because the object is lost after the function is called (so, the modifications are lost). One way to do it is to return a reference to your object in the GetValue function: Date**&** NewDate::GetValue(void) { if (::IsWindow(this->m_hWnd) && GetFocus() == this ) { if( m_dateFormat == shortFormat ) { SetError(!IsValid()); } } return m_value; } But honnestly, I don't really find this very elegant...


      Cédric Moonen Software developer
      Charting control [v1.1]

      1 Reply Last reply
      0
      • M minkowski

        Hi I am trying to pass by reference a variable. Below is the code. In the class def I have void OnLoseFocusDate( Date &jdtDate); and in the src file I have void CEQSLowerInterestLeg::OnLoseFocusDate( Date &jdtDate) { if( jdtDate != Util::grdtNull) { Date adjust_date( jdtDate ); const CCalendar *calendar = CHolidayMgr::GetInstance().GetEquityLegPaymentHoliday(); if (calendar) { adjust_date = calendar->BusinessDayIf(adjust_date, (EnBusDayConvention)m_jcmBDC.GetSelectedObjectID(), &ToolUtils::PromptForBusinessDay); jdtDate = adjust_date; } } } // end of OnLoseFocusDate with the function call being OnLoseFocusDate(m_jdtTerminationDate.GetValue()); where GetValue() is defined as Date NewDate::GetValue(void) { if (::IsWindow(this->m_hWnd) && GetFocus() == this ) { if( m_dateFormat == shortFormat ) { SetError(!IsValid()); } } return m_value; } and m_value is defined as Date m_value; The error I am getting is error C2220: warning treated as error - no object file generated C:\viewstr\ptp6970-DateChange\Swaps\ptp\src\eqslowerinterestleg.cpp(2039) : warning C4239: nonstandard extension used : 'argument' : conversion from 'class Date' to 'class Date &' A reference that is not to 'const' cannot be bound to a non-lvalue I dont understand why I get this error and I dont believe the solution is to pass in by const reference since I modify the argument in the function. Can someone please explain why I get this error and a solution? I have found that if I do Date xx = m_jdtTerminationDate.GetValue(); OnLoseFocusDate(xx); then it is ok but I dont understand the difference between the 2 methods. Many thanks !

        B Offline
        B Offline
        Bram van Kampen
        wrote on last edited by
        #3

        minkowski wrote:

        Date xx = m_jdtTerminationDate.GetValue(); OnLoseFocusDate(xx);

        A Reference is in effect, a Compiler Generated and Guaranteed Pointer. Unlike Ordinary Poiners, they cannot be NULL, and in most cases, when you try to do something daft, the compiler will object. This happened in this case! A Reference implicitly points to a value in memory. The result of a Function is essentially not a value in memory, so the compiler objects, It is WRONG! Your Second attempt works, because the reference is an implicit pointer to xx

        LateNightsInNewry

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups