Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function is not an element of std

Tags:

c++

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

like image 875
EGOrecords Avatar asked Jun 13 '12 15:06

EGOrecords


2 Answers

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
like image 169
Nawaz Avatar answered Oct 06 '22 05:10

Nawaz


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.

like image 31
juanchopanza Avatar answered Oct 06 '22 05:10

juanchopanza