EGOrecords
EGOrecords

Reputation: 1969

function is not an element of std

I got some quite strange errors compiling code under gcc. It tells me that std::function does not exist.

I can recreate the error with the following code:

#include <functional>
#include <stdio.h>

void test(){ printf ("test"); }

int main() {
    std::function<void()> f;
    f = test;
    f();
}

If I run gcc (from cygwin): (my error message was German, so i translated it. It may be sound different on a English gcc)

$ gcc test.cpp
test.cpp: in function "int main(): 
test.cpp:7:3: Error: "function" is not an element of "std"« 
test.cpp:7:25: Error: "f" was not defined in this scope

With MSVC it compiled successfully. Please tell me what I am doing wrong in my code.

Johannes

Upvotes: 8

Views: 19696

Answers (2)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361252

Compile it as:

g++ test.cpp -std=c++0x

-std=c++0x is needed because you're using C++11 features, otherwise g++ test.cpp is enough.

Make sure you've latest version of GCC. You can check the version as:

g++ --version

Upvotes: 18

juanchopanza
juanchopanza

Reputation: 227370

You need to compile in C++ mode, and in C++11 mode. So you need g++ and the -std flag set to c++0x.

g++ test.cpp -std=c++0x

You can also use -std=c++11 from gcc 4.7 onwards.

Upvotes: 3

Related Questions