大家晚上好!
我正在尝试用Microsoft Visual Studio Express 2012在C++中编写多线程应用程序。
我们的想法是,"main“函数调用一个线程,该线程将”永远“运行,任务是更新对象。
这是主要的:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <thread>
#include <iostream>//debug only
#include <fstream> //debug only
#include "dataCollectorFTL.h"
int _tmain(int argc, _TCHAR* argv[])
{
dataCollectorFTL dataCollector1;
//Launch thread which will run forever and get the data flows
dataCollector1.runDataCollector();
while(true){
//application running
}
return 0;
}
这是类的".h“
#ifndef DATACOLLECTORFTL_H_INCLUDED
#define DATACOLLECTORFTL_H_INCLUDED
#include <thread>
class dataCollectorFTL {
public:
void runDataCollector();
void getData();
//constructor, destructor
dataCollectorFTL();
~dataCollectorFTL();
private:
HANDLE hProcess;
std::thread dataCollectorThread;
};
#endif // DATACOLLECTORFTL_H_INCLUDED
最后是".cpp“
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <thread>
#include "dataCollectorFTL.h"
void dataCollectorFTL::runDataCollector(){
//lauch a non-local thread
dataCollectorThread = std::thread(&dataCollectorFTL::getData, this);
}
void dataCollectorFTL::getData(){
//some stuff
}
dataCollectorFTL::dataCollectorFTL(){
//some stuff
}
dataCollectorFTL::~dataCollectorFTL(){
dataCollectorThread.join();
}
问题是,当我运行它时,它给了我这两个错误:
错误1错误运算符:'std::thread::operator =‘C2248:无法访问在类'std::thread’c:\users\damien\documents\visual studio 2012\projects\recherche\recherche\datacollectorftl.h 233 1 Recherche中声明的私有成员
错误4错误C2248:‘std::thread::thread’:无法访问在类'std::thread‘c:\users\damien\documents\visual studio 2012\projects\recherche\recherche\datacollectorftl.h 233 1 Recherche中声明的私有成员
为了节省时间,我可以告诉你:
包含在.h中的
如果我不声明为该类的成员,程序就会崩溃,因为我没有在runDataCollector()中联接()线程。我不想加入它,has ()是一个while(true)函数,它从另一个软体获取数据。
非常感谢您花时间阅读这篇文章,并再次感谢您的帮助。
https://stackoverflow.com/questions/37786966
复制相似问题