The function is not a std element.

I got some rather strange errors compiling code under gcc. He tells me that std::functiondoes 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 ran gcc (from cygwin): (my error message was German, so I translated it. Maybe it is different from 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

+5
source share
2 answers

Compile it as:

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

-std=c++0xnecessary because you are using the features of C ++ 11, otherwise it’s g++ test.cppenough.

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

g++ --version
+13
source

C++ C++11. g++, -std - c++0x.

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

-std=c++11 gcc 4.7 .

+3

All Articles