在派生 class 方法上创建线程会导致错误
Thread creation on a derived class method results in error
我有一个摘要class如下:
class AbstractClass : public std::enable_shared_from_this<AbstractClass> {
public:
virtual ~AbstractClass() = default;
virtual bool Start() = 0;
virtual void Stop() = 0;
};
这是派生的class:
class DerivedClass : public AbstractClass {
public:
bool Start() override;
void Stop() override;
}
我正在尝试在另一个文件中创建派生 class 的对象和派生 class 方法的线程:
// Create object
derivedClass_.reset(...);
//Start a thread for the derived class method
std::unique_ptr<boost::thread> derivedClassThread_;
derivedClassThread_.reset(new boost::thread(std::bind(&DerivedClass::Start,
derivedClass_)));
当我编译这个时,我得到了一些奇怪的错误:
error: no type named 'type' in 'class std::result_of<bool
(DerivedClass::* const&(const volatile
std::shared_ptr&))()>'
有人可以帮我吗?
你的代码不是独立的,所以我们不得不猜测。这是我认为你会 have/want:
#include <boost/thread.hpp>
#include <memory>
#include <iostream>
class AbstractClass : public std::enable_shared_from_this<AbstractClass> {
public:
virtual ~AbstractClass() = default;
virtual bool Start() = 0;
virtual void Stop() = 0;
};
class DerivedClass : public AbstractClass {
public:
bool Start() override {
std::cout << __PRETTY_FUNCTION__ << std::endl;
return true;
}
void Stop() override { }
};
int main()
{
// Create object
std::shared_ptr<AbstractClass> derivedClass_ =
std::make_shared<DerivedClass>();
// Start a thread for the derived class method
auto derivedClassThread_ = std::make_unique<boost::thread>(
[derivedClass_] { derivedClass_->Start(); });
if (derivedClassThread_ && derivedClassThread_->joinable())
derivedClassThread_->join();
derivedClassThread_ = std::make_unique<boost::thread>(
std::bind(&AbstractClass::Start, derivedClass_));
if (derivedClassThread_ && derivedClassThread_->joinable())
derivedClassThread_->join();
}
编译没有问题。
Updated in response to the comment, showed that you can actually do it with std::bind
just the same.
打印:
virtual bool DerivedClass::Start()
virtual bool DerivedClass::Start()
我有一个摘要class如下:
class AbstractClass : public std::enable_shared_from_this<AbstractClass> {
public:
virtual ~AbstractClass() = default;
virtual bool Start() = 0;
virtual void Stop() = 0;
};
这是派生的class:
class DerivedClass : public AbstractClass {
public:
bool Start() override;
void Stop() override;
}
我正在尝试在另一个文件中创建派生 class 的对象和派生 class 方法的线程:
// Create object
derivedClass_.reset(...);
//Start a thread for the derived class method
std::unique_ptr<boost::thread> derivedClassThread_;
derivedClassThread_.reset(new boost::thread(std::bind(&DerivedClass::Start,
derivedClass_)));
当我编译这个时,我得到了一些奇怪的错误:
error: no type named 'type' in 'class std::result_of<bool (DerivedClass::* const&(const volatile std::shared_ptr&))()>'
有人可以帮我吗?
你的代码不是独立的,所以我们不得不猜测。这是我认为你会 have/want:
#include <boost/thread.hpp>
#include <memory>
#include <iostream>
class AbstractClass : public std::enable_shared_from_this<AbstractClass> {
public:
virtual ~AbstractClass() = default;
virtual bool Start() = 0;
virtual void Stop() = 0;
};
class DerivedClass : public AbstractClass {
public:
bool Start() override {
std::cout << __PRETTY_FUNCTION__ << std::endl;
return true;
}
void Stop() override { }
};
int main()
{
// Create object
std::shared_ptr<AbstractClass> derivedClass_ =
std::make_shared<DerivedClass>();
// Start a thread for the derived class method
auto derivedClassThread_ = std::make_unique<boost::thread>(
[derivedClass_] { derivedClass_->Start(); });
if (derivedClassThread_ && derivedClassThread_->joinable())
derivedClassThread_->join();
derivedClassThread_ = std::make_unique<boost::thread>(
std::bind(&AbstractClass::Start, derivedClass_));
if (derivedClassThread_ && derivedClassThread_->joinable())
derivedClassThread_->join();
}
编译没有问题。
Updated in response to the comment, showed that you can actually do it with
std::bind
just the same.
打印:
virtual bool DerivedClass::Start()
virtual bool DerivedClass::Start()