大家晚上好!
我正在尝试使用 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();
}
问题是当我运行它时,它给了我这两个错误:
Error 1 error C2248: 'std::thread::operator =' : cannot access private member declared in class 'std::thread' c:\users\damien\documents\visual studio 2012\projects\recherche\recherche\datacollectorftl.h 233 1 Recherche
Error 4 error C2248: 'std::thread::thread' : cannot access private member declared in class 'std::thread' c:\users\damien\documents\visual studio 2012\projects\recherche\recherche\datacollectorftl.h 233 1 Recherche
为了节省时间,我可以告诉你:
- 包含在 .h 中不会改变任何东西
- runDataCollector 方法的内容没有任何改变。即使它是空的我仍然有问题
- std::thread dataCollectorThread 可以是公共(public)的或私有(private)的,它不会改变任何东西
如果我不声明为该类的成员,程序就会崩溃,因为我没有在 runDataCollector() 中加入 () 线程。我不想加入它,getData() 是一个 while(true) 函数,它从另一个软件获取数据。
非常感谢您花时间阅读本文,再次感谢您的帮助。
最佳答案
我知道那是一段时间以前的事了,但我遇到了具有相同症状的问题,并且能够解决它,因为我的编译器(VS2012 附带的 VC110)对问题的描述有点冗长,所以也许它将帮助某人。
我想使用这个结构:
struct WorkerThreadContext
{
std::thread worker;
... some other attributes ...
}
在 std::vector 中。构建导致错误消息:
d:\code\testroom\t2\t2\t2.cpp(16): error C2248: 'std::thread::thread' : cannot access private member declared in class 'std::thread'
c:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(73) : see declaration of 'std::thread::thread'
c:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(32) : see declaration of 'std::thread'
This diagnostic occurred in the compiler generated function WorkerThreadContext::WorkerThreadContext(const WorkerThreadContext &)'
关键部分是最后一句话 - 编译器似乎无法为我的结构生成隐式复制构造函数,所以我将其更改为:
struct WorkerThreadContext
{
std::thread worker;
... other attributes ...
WorkerThreadContext() {}
// added explicit copy constructor
WorkerThreadContext(const WorkerThreadContext&) {}
};
并且能够编译代码。
编辑:我差点忘了。编译器有问题的原因是,std::thread 对象不能被复制(std::thread::operator=)所以编译器在构造隐式复制构造函数时有问题,因为它不知道如何复制 'std::线程'对象。这也意味着,如果您像我写的那样将显式复制构造函数放在那里,您的成员(包括“std::thread”成员)将变得无效。您当然可以(并且您应该)初始化该结构的其余部分,但是 std::thread 将保持未初始化状态,因为您无法复制它。
关于c++ - 带有 std::thread 的 MVSE12 中的错误 C2248,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37786966/