编辑:正确的标题应该是“在基类中调用重载的真正虚拟函数”,因为这是问题的很大一部分。
我有一个基类,它有一个真正的虚拟函数,还有一些其他的普通函数。其中一个普通的调用线程中的虚拟函数,我在这一行中得到一个错误,我不明白。我猜想它与线程有关,但是由于基类是抽象的,而且每个派生类都必须实际实现虚拟函数,所以应该没有问题。也许是完全不同的东西。这或多或少是它看起来的样子:
class Base {
virtual int getInfo(int a) = 0; // the culprit?
void getInfo(); // is implemented, calls getInfo(int); Does
// actually have the same name. Works perfectly fine.
void getThreadedInfo(); // for details, see below
}
// ..later..
Base::getThreadedInfo() {
...
for(int i=0; i<maxThreads; i++) {
threads.push_back(thread(getInfo, i)); // this is line 85
}
...
}完整的错误消息是:
Error 1 error C3867: 'Base::getInfo': function call missing argument list; use '&Base::getInfo' to create a pointer to member
c:\path\to\base.cpp 85 1 Project
Error 2 error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments
c:\path\to\base.cpp 85 1 Project发布于 2014-06-09 17:23:20
你可以使用这样的方法:
threads.push_back(std::thread(static_cast<int (Base::*)(int)>(&Base::getInfo), this, i));因为&Base::getInfo是模糊的。
发布于 2014-06-09 17:24:30
你有几件不同的事情要做。
第一个最简单的修复方法是第一个错误消息告诉您的内容--您需要在&的参数列表中使用带有getInfo的getInfo操作符来创建函数指针。
第二个问题是,在thread的构造器中调用thread需要一个额外的参数。除了显式int a之外,由于getInfo不是静态的,它需要一个隐式Base* this。因此需要添加this。
第三,最棘手的问题是,Base有两个重载的getInfo,所以您需要明确说明您想要哪一个。
把这三件事结合起来,你需要.就在我打字的时候this answer说了些什么。:)
https://stackoverflow.com/questions/24125356
复制相似问题