Error in the find-and-erase template 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 in Visual C ++ 2005, but compiles using the ARM compiler ("ARM C / C ++ Compiler, RVCT4.0") and 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?

+4
source share
3 answers

try

 typename CONTAINER_TYPE::iterator it ... 
+9
source

Use typename as:

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

Since iterator is a dependent name, and you must tell the compiler that the following follows a type, not a static value.

In C ++ 11, you can just use auto like:

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

What a relief!

+7
source

Dependent Names . MSVS does not see this as an error. You need an extra typename there:

 typename CONTAINER_TYPE::iterator it = eastl::find(cont.begin(), cont.end(), value); 
+3
source

All Articles