【发布时间】:2016-11-11 10:07:19
【问题描述】:
我希望这不是一个愚蠢的问题。请不要投反对票,我是多线程的初学者。
在将参数从类中的函数写入线程时遇到问题。在这里。
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<fstream>
#include<thread>
#include<sstream>
#include<stdio.h>
#include<string.h>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<stdlib.h>
#include <ctime>
//
#include<atomic>
#include<functional>
//
#include <unistd.h>
#include<time.h>
#include"Directory.hpp"
#include"Tests.hpp"
//#define NUM_THREADS 2
using namespace std;
/*
template<class T> void f(T)
void addThreadNoArgs(T) {
thread
}
*/
int main() {
Emotions e;
DictObj d;
//Time t;
//User u;
Self s;
Tests test;
vector < thread > threads;
int i = 200;
Emotions temp;
//pthread_t threads[NUM_THREADS];
//-----------------------------------------------
e.setEmo(50, 50, 25, 50, 40, 50, 30, 20, 10, 20);
temp.setEmo(0,0,0,0,0,0,0,0,0,0);
s.setEmotions(e);
s.setTempEmo(temp);
/*
int rc;
int tc;
rc = pthread_create(&threads[0], NULL, s.tempEmotions, s, e, temp, 200);
tc = pthread_create(&threads[1], NULL, test.testEmoALL, NULL);
*/
//----------------------------------------------
//threads.push_back(thread(&Self::tempEmotions,s,e,temp,200));
thread first(&Self::tempEmotions, s, e, temp, i, ref(s));
thread second(&Tests::testEmoALL, ref(test));
//first.join();
//second.join();
cout << endl << "First and second completed";
//first.detach();
//second.detach();
//----------------------------------------------
return 0;
}
错误
g++ -std=c++11 -Wall -c "NO-DELETE.cpp" -lpthread (in directory: /home/courtneymaroney/Desktop/Courtney/Documents/AI/NEW)
In file included from /usr/include/c++/5/thread:39:0,
from NO-DELETE.cpp:7:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (Self::*)(Self, Emotions, Emotions, int)>(Self, Emotions, Emotions, int, std::reference_wrapper<Self>)>’:
/usr/include/c++/5/thread:137:59: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Self::*)(Self, Emotions, Emotions, int); _Args = {Self&, Emotions&, Emotions&, int&, std::reference_wrapper<Self>}]’
NO-DELETE.cpp:71:57: required from here
/usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Self::*)(Self, Emotions, Emotions, int)>(Self, Emotions, Emotions, int, std::reference_wrapper<Self>)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/5/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Self::*)(Self, Emotions, Emotions, int)>(Self, Emotions, Emotions, int, std::reference_wrapper<Self>)>’
_M_invoke(_Index_tuple<_Indices...>)
^
Compilation failed.
导致问题的行是以下行:thread first(&Self::tempEmotions, s, e, temp, i, ref(s));
我已经尝试在几个地方寻找如何使用几个类参数来解决这个问题,但我似乎找不到答案。线程第二个工作,所以我相信它与论点有关。
编辑(11/8/16):我尝试在源文件和int main() 文件中移动参数,但它不起作用。我仍然有这个问题。我也尝试创建一个新的 Self 类并引用它,但它也没有工作。
ALSO——当我尝试通过按“enter”来分隔代码行中的每个参数时,代码错误被带到最后一部分; ); 存在同样的错误。
【问题讨论】:
标签: c++ multithreading