Pali Pali - 1 year ago 91
C++ Question

C++ strange compiler error if <unistd.h> is included

Everything worked and compiled fine until I

#include <unistd.h>
(this is the single modification in my cpp) which completly breaks everything:

test.cpp:173:33: error: no matching function for call to
‘std::thread::thread(, int, int,
std::thread t1(read, 1, 0, 3),
^ test.cpp:173:33: note: candidates are: In file included from test.cpp:6:0:
/usr/include/c++/4.8/thread:133:7: note: template std::thread::thread(_Callable&&, _Args&& ...)
thread(_Callable&& __f, _Args&&... __args)

[200 more similar lines]

#include <unistd.h>
everything works and compiles but I need it for

Note: I installed ncurses before via apt-get, maybe this broke my env?

Answer Source


std::thread t1(read, 1, 0, 3)

You pass a function pointer to the function read to the constructor of std::thread.

unistd.h declares the function: ssize_t read(int, void *, size_t).

As can be seen from the error message, you have overloads for the read function. The one that you used before including unistd.h is not the same as what unistd.h declares. An overloaded function cannot be implicitly converted into a function pointer, because the compiler cannot know which read function you intended to use.

You can explicitly cast the identifier to a function pointer of correct type to resolve the ambiguity.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download