C++ 将多个参数从函数传递到线程时出现问题
我希望这不是一个愚蠢的问题。请不要对此投反对票,我是多线程的初学者 我遇到了一个问题,当我从一个类的函数向一个线程写入参数时。给你C++ 将多个参数从函数传递到线程时出现问题,c++,multithreading,C++,Multithreading,我希望这不是一个愚蠢的问题。请不要对此投反对票,我是多线程的初学者 我遇到了一个问题,当我从一个类的函数向一个线程写入参数时。给你 #include<iostream> #include<iomanip> #include<vector> #include<string> #include<fstream> #include<thread> #include<sstream> #include<std
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//
#包括
#包括
//
#包括
#包括
#包括“Directory.hpp”
#包括“Tests.hpp”
//#定义NUM_线程2
使用名称空间std;
/*
模板空隙f(T)
void addThreadNoArgs(T){
线
}
*/
int main(){
情绪e;
DictObj d;
//时间t;
//用户u;
自我保护;
测试;
向量<线程>线程;
int i=200;
情绪温度;
//pthread_t threads[NUM_threads];
//-----------------------------------------------
e、 setEmo(50,50,25,50,40,50,30,20,10,20);
温度设定值(0,0,0,0,0,0,0,0,0,0,0);
s、 情绪(e);
s、 塞特佩莫(临时);
/*
int rc;
int tc;
rc=pthread_create(&threads[0],NULL,s.temp,s,e,temp,200);
tc=pthread_create(&threads[1],NULL,test.testemall,NULL);
*/
//----------------------------------------------
//threads.push_back(thread(&Self::temp,s,e,temp,200));
线程优先(&Self::temp、s、e、temp、i、ref);
第二个线程(&测试::testemall,ref(测试));
//首先,join();
//第二,join();
你的定义是什么
成员函数的第一个参数始终是指向此
(您的对象)的指针,除非它是静态函数
我仍然不确定出了什么问题,但我在文件中创建了一个单独的类,并以某种方式解决了它。我确定有更好的解决方案,但这是一个临时修复,直到有人能进一步帮助我。我就是这样做的:
#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 <unistd.h>
#include<time.h>
#include"Directory.hpp"
#include"Tests.hpp"
//#define NUM_THREADS 2
using namespace std;
/*
template<typename _Tp>
class T {
public:
_Tp getT() { return t;}
private:
_Tp t;
};
*/
class RunF {
private:
Emotions e;
DictObj d;
Self s;
Tests test;
Emotions temp;
public:
void setE(Emotions emo) {e = emo;}
void setTE(Emotions emo) {temp = emo;}
void setS(Self emo) {s= emo;}
void setT(Tests emo) {test = emo;}
void setD(DictObj emo) { d = emo;}
//----------------------
void function0() { test.testEmoALL(); }
void function1() {
s.tempEmotions(200, e, temp, s);
}
};
int main() {
Emotions e;
DictObj d;
//Time t;
User u;
Self s;
Tests test;
vector < thread > threads;
Emotions temp;
//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);
//const Self &testSelf();
/*
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));
//--------
RunF f;
f.setE(e);
f.setTE(temp);
f.setS(s);
f.setT(test);
f.setD(d);
//--------
thread first(&RunF::function1, ref(f));
thread second(&Tests::testEmoALL, ref(test));
first.join();
second.join();
cout << endl << "First and second completed";
//first.detach();
second.detach();
//----------------------------------------------
return 0;
}
#包括
#包括
#包括
#包括螺纹;
情绪温度;
//情绪温度;
//pthread_t threads[NUM_threads];
//-----------------------------------------------
e、 setEmo(50,50,25,50,40,50,30,20,10,20);
温度设定值(0,0,0,0,0,0,0,0,0,0,0);
s、 情绪(e);
s、 塞特佩莫(临时);
//const Self&testSelf();
/*
int rc;
int tc;
rc=pthread_create(&threads[0],NULL,s.temp,s,e,temp,200);
tc=pthread_create(&threads[1],NULL,test.testemall,NULL);
*/
//----------------------------------------------
//threads.push_back(thread(&Self::temp,s,e,temp,200));
//--------
RunF;
f、 setE(e);
f、 设定(温度);
f、 套;;
f、 setT(测试);
f、 setD(d);
//--------
线程优先(&RunF::function1,ref(f));
第二个线程(&测试::testemall,ref(测试));
首先,join();
第二,join();
coutvoid tempEmotions(Self-robot,Emotions-original,Emotioms-temp,int-seconds)
问题是最后一个参数:thread first(&Self::tempEmotions,s,e,temp,i,ref(s));
您试图向s发送引用,但tempEmotions函数不需要它。如果我尝试没有ref(s),它说参数的数量不对,仍然会给我错误。你能帮我吗?我应该发布错误以及与之相关的错误吗?尝试清理。创建一个只包含你担心的类/函数的.cpp新文件。继续我上面的评论…在源文件.Ex上编译时发布错误尝试只使用该源文件。尝试更改参数顺序,并查看编译错误的变化。
#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 <unistd.h>
#include<time.h>
#include"Directory.hpp"
#include"Tests.hpp"
//#define NUM_THREADS 2
using namespace std;
/*
template<typename _Tp>
class T {
public:
_Tp getT() { return t;}
private:
_Tp t;
};
*/
class RunF {
private:
Emotions e;
DictObj d;
Self s;
Tests test;
Emotions temp;
public:
void setE(Emotions emo) {e = emo;}
void setTE(Emotions emo) {temp = emo;}
void setS(Self emo) {s= emo;}
void setT(Tests emo) {test = emo;}
void setD(DictObj emo) { d = emo;}
//----------------------
void function0() { test.testEmoALL(); }
void function1() {
s.tempEmotions(200, e, temp, s);
}
};
int main() {
Emotions e;
DictObj d;
//Time t;
User u;
Self s;
Tests test;
vector < thread > threads;
Emotions temp;
//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);
//const Self &testSelf();
/*
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));
//--------
RunF f;
f.setE(e);
f.setTE(temp);
f.setS(s);
f.setT(test);
f.setD(d);
//--------
thread first(&RunF::function1, ref(f));
thread second(&Tests::testEmoALL, ref(test));
first.join();
second.join();
cout << endl << "First and second completed";
//first.detach();
second.detach();
//----------------------------------------------
return 0;
}