gpt4 book ai didi

C++ 未解析的标识符 future

转载 作者:行者123 更新时间:2023-11-30 03:21:12 24 4
gpt4 key购买 nike

我正在学习如何在 C++ 中使用多线程,但遇到了这个问题。我在网上搜索过,没有找到满意的答案。我很确定库的目录包含在内。

这是我的系统规范:

Oracle Virtual box 5.2.12 
Debian 9
Netbeans 8.2
gcc and g++ 6.3.0

这是错误

Cannot find include file "/usr/include/c++/6".
unable to resolve identifier future.
unable to resolve identifier get.

这是我的教程代码:

// async example
#include <iostream> // std::cout
#include <future> // std::async, std::future

// a non-optimized way of checking for prime numbers:
bool is_prime (int x) {
std::cout << "Calculating. Please, wait...\n";
for (int i=2; i<x; ++i) if (x%i==0) return false;
return true;
}

int main ()
{
// call is_prime(313222313) asynchronously:
std::future<bool> fut = std::async (is_prime,313222313);

std::cout << "Checking whether 313222313 is prime.\n";
// ...

bool ret = fut.get(); // waits for is_prime to return

if (ret) std::cout << "It is prime!\n";
else std::cout << "It is not prime.\n";

return 0;
}

这是构建输出:

cd '/home/sulheru/NetBeansProjects/20180907/asyncTutorial'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: se entra en el directorio '/home/sulheru/NetBeansProjects/20180907/asyncTutorial'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/asynctutorial
make[2]: se entra en el directorio '/home/sulheru/NetBeansProjects/20180907/asyncTutorial'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/main.o.d"
g++ -c -g -include /usr/include/c++/6.3.0 -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
cc1plus: fatal error: /usr/include/c++/6.3.0: No existe el fichero o el directorio
compilation terminated.
nbproject/Makefile-Debug.mk:66: fallo en las instrucciones para el objetivo 'build/Debug/GNU-Linux/main.o'
make[2]: *** [build/Debug/GNU-Linux/main.o] Error 1
make[2]: se sale del directorio '/home/sulheru/NetBeansProjects/20180907/asyncTutorial'
nbproject/Makefile-Debug.mk:59: fallo en las instrucciones para el objetivo '.build-conf'
make[1]: *** [.build-conf] Error 2
make[1]: se sale del directorio '/home/sulheru/NetBeansProjects/20180907/asyncTutorial'
nbproject/Makefile-impl.mk:39: fallo en las instrucciones para el objetivo '.build-impl'
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 323ms)

最佳答案

您需要在编译器标志中使用 -pthread 选项。

例子:

➜ g++ -std=c++11 -pthread async.cpp

➜ ./a.out
Checking whether 313222313 is prime.
Calculating. Please, wait...
It is prime!

关于C++ 未解析的标识符 future ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52239808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com