指针矢量;功能失败后

时间:2014-03-27 15:26:45

标签: c++ visual-studio pointers vector

我正在撕裂我的头发。请有人帮帮我吗?

我有一个带有存储对象的向量的C ++类。我有一个函数,每当某个事件发生时都需要运行,它应该获取向量中的最后一个元素并获得它的属性。第一次运行此函数时,向量中显然没有元素,因此该函数表示,如果找不到元素,则创建一个元素并将其添加到向量中。它使用new运算符在动态分配的内存中创建它,然后将指向此对象的push_back()指向向量。

如果,在立即push_back()对象之后我用back()得到它,那么我可以打印出属性。但是,第二次运行该函数时,它将属性作为空字符串(它们都是字符串)。

有谁知道我做错了什么?我的理解是,使用new运算符执行此操作将导致项目得以维护。我也尝试过没有new运算符并且有一个对象的向量而不是指针 - 发生了同样的事情。

vector(std :: vector)存储在包含主要方法的类的对象级别。

这是我需要在事件上运行的函数:

bool CAMMonitoredExtension::LogEvent(const CAMTelephoneEventNotification &camten) {
CAMCall *camCall = NULL;

camCall = GetCallFromNotification(camten);
if (camCall == NULL) {  // Call not found - this is a new call
    CString strNumber;
    strNumber.Format(_T("%s"),camten.m_tcNumber);
    if (strNumber == _T("")) {
        // No phone number, abort
        return false;
    }

    bool bCreateNewIfInvalid = true;
    if (!bCreateNewIfInvalid) { return false;   }

    // Create a new call
    //CAMCall camCallNew(m_strExtension);

    camCall = new CAMCall(m_strExtension);

    camCall->m_uniqueCallID = camten.m_tcCallID; // Identifies call for future runnings of this function.
    camCall->m_strTelephoneNumber = camten.m_tcNumber;
    camCall->m_codtTimeStarted = COleDateTime::GetCurrentTime();
    if (!camCall->Save()) {
        return false;
    }
    // Put this call into the vector and then set the pointer to the reference to that.
    m_vActiveCalls.push_back(camCall);
}
}

这是GetCallFromNotification()函数:

CAMCall* CAMMonitoredExtension::GetCallFromNotification(const CAMTelephoneEventNotification &camteNotification) {
if (m_vActiveCalls.size() == 0 ) {
    return NULL;
}

return m_vActiveCalls.back();
}

CAMCall类声明:

class __declspec(dllexport) CAMCall  
{
public:
    CAMCall(CString strExtension);
    //~CAMCall();

    CAMCall( const CAMCall& rhs );
    CAMCall& operator= (const CAMCall& rhs);
    void CloneFrom(const CAMCall &rhs);
private:
    CAMTrace m_trace;

// properties
public:
    int m_nRowID;

    LPCTSTR m_uniqueCallID;

    CString m_strExtension;
    CString m_strTelephoneNumber;

    COleDateTime m_codtTimeStarted;
    COleDateTime m_codtTimeFinished;

// methods
public:
    bool LogEvent(enumAMTelephoneCallStatus eamtCallStatus);

};

CAMCall类实现:

    CAMCall::CAMCall(CString strExtension)
    :   m_nRowID(-1),
        m_uniqueCallID(_T("DEFAULT CALLID")),
        m_strExtension(strExtension),
        m_strTelephoneNumber(_T("")),
        m_codtTimeStarted(COleDateTime::GetCurrentTime()),
        m_codtTimeFinished((DATE)0)
{

}

CAMCall::CAMCall( const CAMCall& rhs )
{
    try
    {
    CloneFrom( rhs );
    }

    catch(...)
    {
    }
}

CAMCall& CAMCall::operator=(const CAMCall &rhs)
{
    try
    {
    if ( &rhs != this )
    {
        CloneFrom( rhs );
    }
    return *this;
    }

    catch(...)
    {
        CAMCall temp(_T(""));
        return temp;
    }
}

void CAMCall::CloneFrom(const CAMCall &rhs)
{
    m_nRowID = rhs.m_nRowID;
    m_uniqueCallID = rhs.m_uniqueCallID;
    m_strExtension = rhs.m_strExtension;
    m_strTelephoneNumber = rhs.m_strTelephoneNumber;
    m_codtTimeStarted = rhs.m_codtTimeStarted;
    m_codtTimeFinished = rhs.m_codtTimeFinished;
}

0 个答案:

没有答案
相关问题