I am having trouble with the g++ compiler. On my work machine (running OS X 10.10.4) I was experimenting with some code using Xcode. The code did compile succesfully, and the resulting executable works as expected. Output of clang++ --version
:
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.4.0 Thread model: posix
Then I decided to compile this code on a server running Debian 8 with g++. The output of g++ --version
:
g++ (Debian 4.9.2-22) 4.9.2 Copyright (C) 2014 Free Software Foundation, Inc.
The code won't even compile using g++. The command I tried using: g++ -std=c++11 -pthread main.cpp
I get the following error messages:
main.cpp: In function 'int main()':
main.cpp:32:106: error: invalid use of incomplete type 'class std::packaged_task' std::shared_ptr > ptr(new std::packaged_task(std::bind(factorial, 6)));
In file included from main.cpp:11:0:
/usr/include/c++/4.9/future:120:11: error: declaration of 'class std::packaged_task' class packaged_task; ^
main.cpp:33:22: error: variable 'std::future fu1' has initializer but incomplete type std::future fu1 = ptr->get_future(); ^
main.cpp:33:31: error: invalid use of incomplete type 'class std::packaged_task' std::future fu1 = ptr->get_future(); ^
In file included from main.cpp:11:0:
/usr/include/c++/4.9/future:120:11: error: declaration of 'class std::packaged_task' class packaged_task; ^
main.cpp: In lambda function:
main.cpp:34:48: error: invalid use of incomplete type 'class std::packaged_task' std::function task1 = &ptr{ ptr->operator()(); }; ^
In file included from main.cpp:11:0:
/usr/include/c++/4.9/future:120:11: error: declaration of 'class std::packaged_task' class packaged_task; ^
main.cpp: In function 'int main()':
main.cpp:36:38: error: variable 'std::packaged_task t' has initializer but incomplete type std::packaged_task t(std::bind(factorial, 5)); ^
main.cpp:37:22: error: variable 'std::future fu2' has initializer but incomplete type std::future fu2 = t.get_future(); ^
My code:
#include <iostream>
#include <thread>
#include <future>
#include <memory>
using std::cout;
using std::cin;
using std::endl;
unsigned long long int factorial(unsigned long long int num)
{
unsigned long long int N = num;
for (unsigned long long int i = num; i > 1; --i)
{
num *=(--N);
}
return num;
}
int main()
{
std::shared_ptr<std::packaged_task<int()> > ptr(new std::packaged_task<int()>(std::bind(factorial, 6)));
std::future<int> fu1 = ptr->get_future();
std::function<void()> task1 = [&ptr](){ ptr->operator()(); };
std::packaged_task<int()> t(std::bind(factorial, 5));
std::future<int> fu2 = t.get_future();
std::function<void()> task2 = [&t](){ t(); };
std::thread threads[2];
threads[0] = std::thread(task1);
threads[1] = std::thread(task2);
cout << fu1.get() << endl;
cout << fu2.get() << endl;
threads[0].join();
threads[1].join();
return 0;
}
What could be the issue with g++?
<functional>
;) – Pixelchemist<functional>
and got the same error message – krispet krispetfunctional
. But nvm if it doesn't help anyway. – Stephan Dollberg