繁体   English   中英

MVSE12中带有std :: thread的错误C2248

[英]Error C2248 in MVSE12 with std::thread

各位晚上好 !

我正在尝试使用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错误C2248:'std :: thread :: operator =':无法访问在类'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方法的内容没有任何改变。 即使它是空的,我仍然有问题
  • std :: thread dataCollectorThread可以是公共的或私有的,它不会更改任何内容

如果我不声明为该类的成员,则程序会崩溃,因为我没有在runDataCollector()中加入join()线程。 而且我不想加入它,因为getData()是while(true)函数,它从另一个软件获取数据。

非常感谢您花时间阅读本文,并再次感谢您提供的任何帮助。

我知道那是一段时间前的事,但是我遇到了同样症状的问题,并且能够解决它,因为我的编译器(VS2012附带的VC110)对问题有更多的详细说明,因此也许会有所帮助有人

我想使用此结构:

struct WorkerThreadContext
{
   std::thread worker;
   ... some other attributes ...
}

在std :: vector中。 生成导致错误消息:

d:\\ code \\ testroom \\ t2 \\ t2 \\ t2.cpp(16):错误C2248:'std :: thread :: thread':无法访问在类'std :: thread'中声明的私有成员

c:\\ program files(x86)\\ Microsoft Visual Studio 11.0 \\ vc \\ include \\ thread(73):请参见'std :: thread :: thread'的声明

c:\\ program files(x86)\\ Microsoft Visual Studio 11.0 \\ vc \\ include \\ thread(32):请参见'std :: thread'的声明

此诊断发生在编译器生成的函数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 :: thread”对象。 这也意味着,如果在其中放置显式的副本构造函数作为我编写的副本构造函数,则您的成员(包括'std :: thread'一个成员)将无效。 您可以(当然应该)初始化结构的其余部分,但是std :: thread将保持未初始化状态,因为您无法复制它。

当您将一个函数传递给std::thread构造函数时,需要可以从文件范围调用该函数。 换句话说,如果它是成员函数,则该函数必须为static 您仍然可以传递this -只是告诉函数期望它。

换一种说法:

  • 将以下内容添加到类声明中:

     private: void getData(); // Moved from public! static void myGetData(DataCollectorFTL *ftl); 
  • myGetData

     void DataCollectorFTL::myGetData(DataCollectorFTL *ftl) { ftl->getData(); } // DataCollectorFLT::myGetData(ftl) 
  • 将调用更改为std::thread()

     //lauch a non-local thread dataCollectorThread = std::thread(&myGetData, this); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM