【问题标题】:Errors with std list标准列表错误
【发布时间】:2011-10-06 21:47:57
【问题描述】:

由于某种原因,我在 ErrorHandler.h 中不断收到以下错误 为什么 size 函数缺少参数?

'std::list<_ty>::size': 函数调用缺少参数列表;使用 '&std::list<_ty>::size' 创建指向成员的指针

    'std::_List_iterator<_Mylist> std::list<_Ty>::erase(std::_List_const_iterator<_Mylist>,std::_List_const_iterator<_Mylist>)' : cannot convert parameter 1 from 'int' to 'std::_List_const_iterator<_Mylist>' 

    'std::_List_iterator<_Mylist> std::list<_Ty>::erase(std::_List_const_iterator<_Mylist>)' : cannot convert parameter 1 from 'int' to 'std::_List_const_iterator<_Mylist>'


// in errorhandler.h

    class ErrorHandler{
        std::list<unsigned int> m_ErrorList;
    public:
        ErrorHandler(){ }
        ~ErrorHandler(){ }
        void ForceShutdown(){ free(&m_ErrorList); }
        void Add(int errCode){ m_ErrorList.push_back(errCode); }
        unsigned int GetLastError(){ if(m_ErrorList.size!=0)return m_ErrorList.back(); }
        void Remove(int pos){ if(m_ErrorList.size!=0)m_ErrorList.erase(pos); }
        void RemoveRange(int start,int end){ if(m_ErrorList.size!=0)m_ErrorList.erase(start,end); }

    };


// in criticalsection.h
    class CriticalSection{
        long m_nLockCount;
        long m_nThreadId;
        typedef CRITICAL_SECTION cs;
        cs m_tCS;
    public:
        CriticalSection(){
            ::InitializeCriticalSection(&m_tCS);
            m_nLockCount = 0;
            m_nThreadId = 0;
        }
        ~CriticalSection(){ ::DeleteCriticalSection(&m_tCS); }
        void Enter(){ ::EnterCriticalSection(&m_tCS);  }
        void Leave(){  ::LeaveCriticalSection(&m_tCS); }
        void Try();
    };
    class LockSection{
        CriticalSection* m_pCS;
        ErrorHandler * m_pErrorHandler;
    public:
        LockSection(CriticalSection* pCS,ErrorHandler* pErrorHandler){
            m_pCS = pCS;
            m_pErrorHandler = pErrorHandler;
            if(!m_pCS)m_pErrorHandler->Add(0x1AE1); // 0x1AE is code prefix for critical section header
            if(m_pCS)m_pCS->Enter();
        }
        ~LockSection(){
            if(!m_pCS)m_pErrorHandler->Add(0x1AE2);
            if(m_pCS)m_pCS->Leave();
        }
    };

【问题讨论】:

    标签: c++ list parameters std


    【解决方案1】:

    http://www.cplusplus.com/reference/stl/list/pop_back/
    不,pop_back 不返回最后一个元素。这是为了防止意外错误。您必须通过back() 显式获取最后一个元素。如果您想在不阅读它们的情况下弹出几个,这种方式也更快。这也适用于所有其他标准 C++ 库容器。

    从您的警告来看,您似乎也无法删除。对于列表,这可能很棘手:

    void Remove(int pos){
        std::list<unsigned int>::const_iterator iter = m_ErrorList.begin();
        //no need to check the size, advance will throw an exception if pos is invalid
        std::advance(iter, pos);
        m_ErrorList.erase(iter);
    }
    

    【讨论】:

    • pop_back() 不会返回最后一个元素,因为它必须按值返回,这可能效率低下——尽管 C++11 移动构造表面上可以解决这个问题。
    • @JonPurdy:这不仅仅是关于效率;这是关于异常正确性的。如果复制ctor抛出异常,返回被弹出对象的弹出将丢失该对象。
    • @JerryCoffin:另一个值得考虑的好事情。虽然,为什么复制构造函数会首先抛出是更令人担忧的。
    • @JonPurdy: 喜欢std::bad_alloc?
    【解决方案2】:

    你使用列表方法很糟糕:

    if(m_ErrorList.size!=0)
    

    size是一个方法,所以需要调用它(带括号):

    if(m_ErrorList.size()!=0)
    

    注意sizelist 来说很慢;您可能希望像这样实现 GetLastError:

    unsigned int GetLastError(){ if(!m_ErrorList.empty())return m_ErrorList.back(); }
    

    m_ErrorList.erase(pos);
    

    erase 采用迭代器,而不是整数。因此,您最好使用

    std::list::iterator it=m_ErrorList.begin();
    std::advance(it, pos);
    m_ErrorList.erase(it);
    

    请注意,这也不是一种特别有效的方法。

    顺便说一句,检查你是否需要listvector 可能会更好地为您服务。

    【讨论】:

    • 我不认为std::list::iterator 重载operator+。我认为他必须使用std::advance
    • @MooingDuck:是的。这使得笨拙更加明显。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    相关资源
    最近更新 更多