Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in templated "find-and-erase" function

template <typename CONTAINER_TYPE, typename CONTAINER_VALUE_TYPE>
bool FindAndErase(CONTAINER_TYPE& cont, const CONTAINER_VALUE_TYPE& value)
{
    CONTAINER_TYPE::iterator it = eastl::find(cont.begin(), cont.end(), value);
    if (it != cont.end())
    {
        cont.erase(it);
        return true;
    }
    return false;
}

This code compiles fine on Visual C++ 2005, but compiling using an ARM compiler ("ARM C/C++ Compiler, RVCT4.0") and the iOS gcc ("arm-apple-darwin9-gcc (GCC) 4.2.1") returns errors:

Error: #65: expected a ";" Error: #20: identifier "it" is undefined

in the 4th and 5th lines respectively.

What is wrong with this code?

like image 364
GhassanPL Avatar asked Dec 01 '22 23:12

GhassanPL


2 Answers

try

typename CONTAINER_TYPE::iterator it ...
like image 125
Torsten Robitzki Avatar answered Dec 04 '22 13:12

Torsten Robitzki


Use typename as:

typename CONTAINER_TYPE::iterator it = //...

Because iterator is a dependent name and you need to tell the compiler that what follows is a type, not static value.

In C++11, you could just use auto as:

auto it = eastl::find(cont.begin(), cont.end(), value);

What a relief!

like image 40
Nawaz Avatar answered Dec 04 '22 12:12

Nawaz