[英]std::thread::thread(<unresolved overloaded function type>) in Qt
The other answers I read on this website didn't seem relevant to my issue. 我在该网站上阅读的其他答案似乎与我的问题无关。 Anyway, in my mainwindow.cpp file I have a function that needs access to the ui namespace:
无论如何,在我的mainwindow.cpp文件中,我有一个需要访问ui名称空间的函数:
do
{
intrecv= recv(s1, buffer, 10000,0);
if(intrecv > 0)
{
recvData = buffer;
ui->textEdit->setText("Connection occurred.\n");
}
}while(intrecv > 0);
Here are my preprocessor commands: 这是我的预处理器命令:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <thread>
#include <string>
#include <QString>
In my mainwindow.h file I prototyped the receive function as such: 在我的mainwindow.h文件中,接收函数原型如下:
class MainWindow : public QMainWindow
{
Q_OBJECT
void receiveFunction();
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_lineEdit_returnPressed();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Then, in mainwindow.cpp, I called that receive function in a thread in another function: 然后,在mainwindow.cpp中,我在另一个函数的线程中调用了该接收函数:
void MainWindow::on_pushButton_clicked()
{
connectFunction();
std::thread t1(receiveFunction);
t1.detach();
}
An example you could try to replicate my problem is make a pushbutton and a textEdit field. 您可以尝试复制我的问题的一个示例是创建一个按钮和一个textEdit字段。 Have the pushbutton's slot call a function that edits the textEdit field.
让按钮的插槽调用一个用于编辑textEdit字段的函数。
edit: I forgot to mention: for my mini example, don't just have it edit the textEdit field, have it receive data and edit the textEdit field. 编辑:我忘了提一下:在我的迷你示例中,不仅仅是让它编辑textEdit字段,让它接收数据并编辑textEdit字段。
The error message seems to hint that there are multiple overloads of the reveiveFunction
function, but that might be misleading (if the code above is correct). 错误消息似乎暗示
reveiveFunction
函数有多个重载,但这可能会引起误解(如果上面的代码正确)。 There are two things inherently wrong in the line: 该行有两个固有的错误:
std::thread t1(receiveFunction);
First is that you cannot take the address of a member function without providing the type qualification (and a member function does not decay to a pointer to member function). 首先,如果不提供类型限定条件,就不能获取成员函数的地址(并且成员函数不会衰减到指向成员函数的指针)。 The second problem is that a non-static member function requires an object on which it will be called, which you are not providing.
第二个问题是,非静态成员函数需要一个您将不提供的对象,该对象将在该对象上被调用。 That statement can be fixed by doing:
该语句可以通过执行以下操作来修复:
std::thread t1(&MainWindow::receiveFunction, this);
That will correctly take the address of the member function, and provide the this
pointer on which the function will be evaluated. 这将正确地获取成员函数的地址,并提供将在其上评估函数的
this
指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.