我正在使用一个Visual Studio项目(v120编译器),它使用std :: thread从GUI旁边的usb设备读取,并且该函数抛出一个错误:“错误C2661'std :: thread :: thread ':没有重载函数需要3个参数“
这是代码:
class IOThread
{
public:
IOThread(DeviceHandler *handle) : _handle(handle)
~IOThread();
std::thread *getThread(ThreadType type);
template <typename T>
void execRead(std::list<T> *dataStack)
{
std::thread *thread = getThread(Read);
if (thread == NULL)
{
thread = new std::thread(&DeviceHandler::readFromBus, _handle, dataStack);
_threadPool.push_back(std::make_pair(Read, thread));
}
}
private:
DeviceHandler *_handle;
std::vector<std::pair<ThreadType, std::thread *>> _threadPool;
};
此外,DeviceHandler是一个抽象类,它定义了纯虚拟的readFromBus函数,其原型如下
template <typename T>
void readFromBus(std::list<T> *dataStack) = 0;
我希望你在解决这个烂摊子时不要像我一样头疼... 的问候,
答案 0 :(得分:2)
如评论中所述,您的情况与this question中的情况相同。因为方法DeviceHandler::readFromBus()
是任意模板化的,所以可以生成许多重载。 (他们共享名称但有不同的签名)。
因此,编译器无法选择正确的重载,因此错误消息。您需要告诉编译器转换器使用哪个重载。 (正如this answer解释)
以下演员应该这样做:
thread = new std::thread(
static_cast<void (DeviceHandler::*)(std::list<T> *)>(&DeviceHandler::readFromBus),
_handle, dataStack);
答案 1 :(得分:0)
我试图给出错误的MVCE,但我无法测试它是否编译; 但这里是使用你的演员
的类的实际结构thread = new std::thread(
static_cast<void (DeviceHandler::*)(std::list<T> *)>(&DeviceHandler::readFromBus),
_handle, dataStack);
编辑:我解决了问题,问题是模板化的纯定义,我用一个接受参数抽象结构的函数替换如下
typedef struct s_dataStack
{
DataType type;
std::list<void *> stack;
} t_dataStack;
然后我在枚举&#34;数据类型&#34;中使用提供的类型转换任何堆栈元素。 感谢您提供的帮助,它让我了解了问题的根源。