简体   繁体   中英

g++: expected primary-expression before ‘[’ token

I am trying to find if all the elements of a vector y is equal to 1. The following code works fine in Visual Studio but with g++ in linux (g++ -std=c++0x) it gives me this error: expected primary-expression before '[' token

bool x = all_of(y.begin(), y.end(), [](unsigned char j) {return j == 1;});

Any help would be appreciated.

My gcc version is: g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17)

Lambdas are not supported in GCC 4.4 .

Upgrade your compiler. You need GCC 4.5 or higher, but get to modern times if you can.

Your compiler not support lambda expression. Compilers do not support all features that come with C++11 or new incoming standards. Therefore you need to check which standards the compiler supports. GCC 4.4.7上的编译屏幕截图

You can see if I select gcc-4.4.7 same error( lambda expression error) but if I select gcc-4.5.3

GCC 4.5.2上的编译屏幕截图

No error. In summary, You have to change your compiler( >= gcc-4.5 ) to use lamda expression.

GCC中的C ++标准支持

Lambdas are not supported in GCC 4.4. You can upgrade your compiler to version 4.5 or above, or use a function:

bool compFun(int i) {
    return i == 1;
}
...
bool res = all_of(a.begin(), a.end(), compFun);

Here is a live example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM