Kirill Danshin
Kirill Danshin

Reputation: 67

C++ error: expected primary-expression before ‘[’ token

Now I'm trying to install RealPlexor by dklab, but it's falls with errors:

# bash ./Make.sh 
In file included from dklab_realplexor.cpp:68:
utils/misc.h: In function ‘void die(std::string)’:
utils/misc.h:105: error: expected primary-expression before ‘[’ token
compilation terminated due to -Wfatal-errors.

Here is that line

s = regex_replace(s, regex("\\$!"), [](smatch s) { return strerrno(); });

Upvotes: 2

Views: 9264

Answers (2)

DavidO
DavidO

Reputation: 13942

Make sure that you are passing the following flag to your compiler (as described in the the g++ documentation):

-std=c++11

This tells the gcc compiler (g++) to compile your code with C++11 semantics.

The lambda expression syntax you are using (the part starting with []) is a C++11 feature, and will cause compilers great confusion if it appears in code that they aren't expecting to be C++11.

However, as has been pointed out in another comment here (and is confirmed by this table, the version of gcc you are running (4.4.5, per a comment) doesn't have lambda expression support. May have to use a function object instead, or upgrade to a newer version of gcc/g++.

Upvotes: 3

Lawrence Aiello
Lawrence Aiello

Reputation: 4668

Just say

s = regex_replace(s, regex("\\$!"), *(smatch s) { return strerrno(); });

The [] operator is usually used to index something (like a character array), so C++ expects something in front of it

Also try this suggestion from @DavidO:

You're using a lambda expression which is a C++11 syntax, but probably haven't set your compiler to recognize C++11. If you're using g++, you would use the -std=c++11 flag.

Upvotes: 0

Related Questions