如何正确创建线程类的子类 [英] How to create a subclass of thread Class properly
问题描述
我正在尝试创建一个类Parallel,它是线程类的子类,因此我的类在Parallel.h中定义,但我的main方法在同一个项目中的单独文件main.cpp中定义(在visual studio中。当我创建一个Parallel类的实例并在main方法中执行join()函数时,如下面的代码段:
//Parallel.h
#include< thread>
使用 命名空间标准;
命名空间 Para {
class 并行:thread
{
public :
static void run(){
}
Parallel( void )
{
}
虚拟〜并行( void )
{
}
inline static void start(Parallel * p){
// (* p).join();
}
virtual void Parallel :: start(thread& t){
}
静态 < span class =code-keyword> void parallelize(Parallel& P1,并行与安培; p2){
}
inline virtual Parallel * operator = (thread * t){
return static_cast< Parallel *>(t);
}
}
;
}
//main.cpp
#include< iostream>
#include Parallel.h
#include Resource.h
使用 命名空间标准;
使用 命名空间 Para;
void main(){
parallel p;
p。 join ();
thread t(print);
t。 join ();
// cout<< p.get_id()<<\ n;
system( 暂停);
}
Resource.h中定义的打印功能
问题是如何定义一个具有重载构造函数的线程类的正确子类,该函数名称作为参数,也是
p.join()
给出了以下编译错误:
错误2错误C2247:'std :: thread :: join'无法访问,因为'Para :: Parallel'使用' private'继承自'std :: thread'C:\ Users \ Gamer \Desktop\PROJECQ \ VCC ++ @ OMAQ\CQ47 \CQ47 \ main.cpp 11
3智能感知:函数std :: thread :: join(在H:\Program Files(x86)\ Microsoft Visual Studio 11.0 \ VC的第209行声明\\ thread)无法访问c:\ Users \ Gamer \Desktop \ PROJECQ \ VC ++ @ OMAQ\CQ47 \CQ47 \ main.cpp 11
如何正确构造线程类的子类
您需要将公共范围属性转发到您继承的类,因此:
class Parallel: public thread
{
I am trying to create a class "Parallel" which is a subclass of thread class,therefore my class is defined in "Parallel.h",but my main method defined in separate file "main.cpp" in same project(in visual studio).When I create an instance of Parallel class and execute join() function in main method as below code segment:
//Parallel.h
#include <thread>
using namespace std;
namespace Para{
class Parallel:thread
{
public:
static void run(){
}
Parallel(void)
{
}
virtual ~Parallel(void)
{
}
inline static void start(Parallel* p){
// (*p).join();
}
virtual void Parallel::start(thread& t){
}
static void parallelize(Parallel& p1,Parallel& p2){
}
inline virtual Parallel* operator=(thread* t){
return static_cast<Parallel*>(t);
}
}
;
}
//main.cpp
#include <iostream>
#include "Parallel.h"
#include "Resource.h"
using namespace std;
using namespace Para;
void main(){
Parallel p;
p.join();
thread t(print);
t.join();
//cout<<p.get_id()<<"\n";
system("Pause");
}
print function defined in "Resource.h"
Problem is how to define a proper subclass of a thread class having a overloaded constructor taking function name as a parameter,also "
p.join()
" gave following compile errors:
Error 2 error C2247: 'std::thread::join' not accessible because 'Para::Parallel' uses 'private' to inherit from 'std::thread' C:\Users\Gamer\Desktop\PROJECQ\VC++@OMAQ\CQ47\CQ47\main.cpp 11
3 IntelliSense: function "std::thread::join" (declared at line 209 of "H:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\thread") is inaccessible c:\Users\Gamer\Desktop\PROJECQ\VC++@OMAQ\CQ47\CQ47\main.cpp 11
How to construct a subclass of thread class properly
You need to carry the public scope attributes forward to your inherited class, thus:
class Parallel: public thread {
这篇关于如何正确创建线程类的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!