1

in a file OrbCudaTracker.h I have a class OrbCudaTracker with a member function ProcessNewFrame, as follows:

class OrbCudaTracker
{
public:
  OrbCudaTracker();
  ~OrbCudaTracker();
  void ProcessNewFrame(const cv::cuda::GpuMat &frame, MatchingResult &resCam1, MatchingResult &resCam2, int buffInd);
};

from main.cpp, I would like to start a separate thread running ProcessNewFrame from an object of class OrbCudaTracker, as follows:

OrbCudaTracker tracker;
MatchingResult r1, r2;
int buffInd = 0;
cuda::GpuMat cudaCurrImage;
std::thread tProcess(&OrbCudaTracker::ProcessNewFrame, &tracker, cudaCurrImage, r1, r2, buffInd);
/*Another thread starting here*/
tProcess.join();
tAnother.join();

However, when I compile I get the following error message:

/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (OrbCudaTracker::*)(const cv::cuda::GpuMat&, MatchingResult&, MatchingResult&, int)>(OrbCudaTracker*, cv::cuda::GpuMat, MatchingResult, MatchingResult, int)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                         ^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (OrbCudaTracker::*)(const cv::cuda::GpuMat&, MatchingResult&, MatchingResult&, int)>(OrbCudaTracker*, cv::cuda::GpuMat, MatchingResult, MatchingResult, int)>’
         _M_invoke(_Index_tuple<_Indices...>)

I am compiling on Ubuntu 14.04.1 LTS with gcc 4.8.4 on a Tegra TX1. In my cmake file I have enabled the -std=c++11 flag. I have followed the syntax explained here:

How to, in C++11, use std::async on a member function?

But I get the compilation error reported above. Any help would be very much appreciated. Thank you for your time.

Update: I have tried to replace the line:

std::thread tProcess(&OrbCudaTracker::ProcessNewFrame, &tracker, cudaCurrImage, r1, r2, buffInd);

with

std::thread tProcess(&OrbCudaTracker::ProcessNewFrame, std::ref(tracker), cudaCurrImage, r1, r2, buffInd);

as shown in the link suggested in the comments. Now the compilation error looks like:

/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (OrbCudaTracker::*)(const cv::cuda::GpuMat&, MatchingResult&, MatchingResult&, int)>(std::reference_wrapper<OrbCudaTracker>, cv::cuda::GpuMat, MatchingResult, MatchingResult, int)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (OrbCudaTracker::*)(const cv::cuda::GpuMat&, MatchingResult&, MatchingResult&, int)>(std::reference_wrapper<OrbCudaTracker>, cv::cuda::GpuMat, MatchingResult, MatchingResult, int)>’
         _M_invoke(_Index_tuple<_Indices...>)
Community
  • 1
  • 1
Fcom
  • 91
  • 6
  • 1
    I suspect that your old version of gcc does not fully support all C++11 features. I see nothing wrong with the code. Try a more recent compiler. – Sam Varshavchik Oct 11 '16 at 12:39
  • When you run into a compiler error like this, before coming to stack overflow try to simplify it. Create a toy class with just the method that does nothing (same signature). Rebuild, check that the error is the same. Now replace any arguments, one at a time, with simpler ones. Rebuild after each simplification. Back up a simplification if the error changes substantially, and simplify somewhere else. Replace specific types with generic ones (either `struct foo{}` or `int` or whatever), strip references and qualifiers, remove arguments entirely. Then come here with remaining problem. – Yakk - Adam Nevraumont Oct 11 '16 at 12:43
  • @sam no, the code is broken. As a warning, some versions of msvc are broken in a complimentary way that lets the above code compile, but not work sensibly. – Yakk - Adam Nevraumont Oct 11 '16 at 12:43
  • @sam tahnk you for the suggestion, I will try with a newer compiler. – Fcom Oct 11 '16 at 14:23
  • @Yakk thank you for the piece of advice regarding more appropriate ways to post on Stack Overflow. Could you please elaborate more on the reason why "the code is broken"? – Fcom Oct 11 '16 at 14:24
  • @NathanOliver is it possible to see which question represents the exact duplicate of mine? (Sorry I have now seen the link on the top of the page) – Fcom Oct 11 '16 at 14:25
  • It is at the top of the question in the banner but here you go: http://stackoverflow.com/questions/34078208/passing-object-by-reference-to-stdthread-in-c11 – NathanOliver Oct 11 '16 at 14:27
  • @NathanOliver I have replaced the line: std::thread tProcess(&OrbCudaTracker::ProcessNewFrame, &tracker, cudaCurrImage, r1, r2, buffInd); with std::thread tProcess(&OrbCudaTracker::ProcessNewFrame, std::ref(tracker), cudaCurrImage, r1, r2, buffInd); as pointed out in the link oyu have shown me, but I still get the ocmpilation error – Fcom Oct 11 '16 at 14:32
  • You need to use `std::ref` for every reference parameter. You have more then 1. – NathanOliver Oct 11 '16 at 14:33
  • something like: std::thread tProcess(&OrbCudaTracker::ProcessNewFrame, std::ref(tracker), std::ref(cudaCurrImage), std::ref(r1), std::ref(r2), buffInd); ? I still get compilation error – Fcom Oct 11 '16 at 14:42
  • @Fcom There is a link that solves at least one problem in your code. There are steps that state how you can reduce your problem down to [MCVE]. Doing so both makes it easier to find the error in your code, and may let you solve the problem yourself. "Here is some code, not all of it, just what I think matters, it don't work, fix it" is not going to get you help. Short of us rebuilding your project (maybe in our head) without your project, including your exact line, generating the error, modifying the line, posting the modified line, and you saying "thanks" that is. – Yakk - Adam Nevraumont Oct 11 '16 at 14:45
  • Oh, I wouldn't wrap `tracker` in `ref`. It isn't a reference parameter to the method pointer. Maybe it works, but I wouldn't do it myself, so maybe it does't work. – Yakk - Adam Nevraumont Oct 11 '16 at 14:49
  • @Yakk thank you for the information. – Fcom Oct 13 '16 at 07:23
  • @NathanOliver thank you for pointing me to the answer. I was not understanding the nature of the error, but the answer you have pointed out made it clearer. Would it be possible to ask your opinion regarding a good reference (online tutorial, book...) where to learn more about multi-threading in C++11? Thank you for your time – Fcom Oct 13 '16 at 07:29
  • @Fcom Check out: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – NathanOliver Oct 13 '16 at 11:32

0 Answers0