class + std::async not working together.

Can't get even to compile this. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <vector>
#include <future>
using namespace std;
class A
{
	
	int a,b;
	public:	
	A(int i=1, int j=2){ a=i; b=j;} 

	std::pair<int,int> do_rand_stf(int x,int y)
	{
		std::pair<int,int> ret(x+a,y+b);
		return ret;
	}
	
	void run()
	{
		std::vector<std::future<std::pair<int,int>>> ran;
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				auto hand=async(launch::async,do_rand_stf,i,j);
				ran.push_back(hand);	
			}
		}
		for(int i=0;i<ran.size();i++)
		{
			pair<int,int> ttt=ran[i].get();
			cout << ttt.first << ttt.second << endl;
		} 
	}
};
int main()
{
	A a;
	a.run();
}


edit: compilation options:
 
g++ -std=c++11 -pthread main.cpp 
Last edited on
Given that we don't know what's in that <future> header you've got, it's a bit tricky to say. I can see, however, that you're not linking against anything else. Does <future> contain the actual code for do_rand_stf as well as the declaration? Is the error message "Unresolved External"?

Come on, give us a chance. What's the error message?
Last edited on
<future> comes with C++11. do_rand_stf is defined just above run().
1
2
3
4
5
	std::pair<int,int> do_rand_stf(int x,int y)
	{
		std::pair<int,int> ret(x+a,y+b);
		return ret;
	}


The error message is quite "not simple".

In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/move.h:57:0,
                 from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/stl_pair.h:61,
                 from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/stl_algobase.h:65,
                 from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/vector:61,
                 from main.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/type_traits: In instantiation of 'struct std::_Result_of_memfun<std::pair<int, int> (A::*)(int, int), int&, int&>':
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/type_traits:1847:12:   required from 'struct std::_Result_of_impl<false, true, std::pair<int, int> (A::*)(int, int), int&, int&>'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/type_traits:1857:12:   required from 'class std::result_of<std::pair<int, int> (A::*(int&, int&))(int, int)>'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/future:1486:5:   required by substitution of 'template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...) [with _Fn = std::pair<int, int> (A::*)(int, int); _Args = {int&, int&}]'
main.cpp:61:50:   required from here
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/type_traits:1823:9: error: cannot apply member pointer 'std::declval<std::pair<int, int> (A::*)(int, int)>()' to 'std::_Result_of_memfun<_Res _Class::*, _Arg, _Args ...>::_S_get<int&>((* & A(std::declval<int&>(), 2)))', which is of non-class type 'int'
main.cpp: In member function 'void A::run()':
main.cpp:61:50: error: no matching function for call to 'async(std::launch, <unresolved overloaded function type>, int&, int&)'
main.cpp:61:50: note: candidates are:
In file included from main.cpp:3:0:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/future:1486:5: note: template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/future:1486:5: note:   substitution of deduced template arguments resulted in errors seen above
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/future:1507:5: note: template<class _Fn, class ... _Args> typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type std::async(_Fn&&, _Args&& ...)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/future:1507:5: note:   template argument deduction/substitution failed:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/future: In substitution of 'template<class _Fn, class ... _Args> typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type std::async(_Fn&&, _Args&& ...) [with _Fn = std::launch; _Args = {std::pair<int, int> (A::*)(int, int), int&, int&}]':
main.cpp:61:50:   required from here
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/future:1507:5: error: no type named 'type' in 'struct std::__async_sfinae_helper<std::launch, std::launch, std::pair<int, int> (A::*)(int, int), int&, int&>'
main.cpp:61:50: error: unable to deduce 'auto' from '<expression error>'
Asked the question in stackoverflow and got the answer there.

http://stackoverflow.com/questions/11758414/class-and-stdasync-on-class-member-in-c

in short:

1
2
auto hand=async(launch::async,do_rand_stf,i,j);
				ran.push_back(hand);

should be changed to:

1
2
ran.push_back(async(launch::async,&A::do_rand_stf,this,i,j));
Topic archived. No new replies allowed.