NT_SYSTEM
NT_SYSTEM

Reputation: 377

Errors with std list

For some reason, I keep getting the following errors in ErrorHandler.h why size function is missing arguments?

'std::list<_Ty>::size': function call missing argument list; use '&std::list<_Ty>::size' to create a pointer to member

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

Upvotes: 0

Views: 3554

Answers (2)

jpalecek
jpalecek

Reputation: 47770

You use the list methods badly:

if(m_ErrorList.size!=0)

size is a method, so you need to call it (with parentheses):

if(m_ErrorList.size()!=0)

Note that size is slow for list; you may want to implement GetLastError like this:

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

m_ErrorList.erase(pos);

erase takes an iterator, not an integer. Therefore, you'd better use

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

note that this isn't a particularly efficient way, either.

BTW, check that you need list; a vector might serve you better.

Upvotes: 0

Mooing Duck
Mooing Duck

Reputation: 66942

http://www.cplusplus.com/reference/stl/list/pop_back/
Nope, pop_back does not return the last element. This is to prevent accidental errors. You have to get the last element explicitly via back(). This way is also faster if you want to pop several without reading them. This also applies to all the other Standard C++ Library containers.

Judging by your warnings, it looks like you're also having trouble deleting. For Lists it can be tricky:

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

Upvotes: 1

Related Questions