首页 > 解决方案 > c++ boost::thread() in Qt application - error: too many arguments to function

问题描述

I am trying to integrate the functionality of boost::thread in my Qt applications but the compiler produces an error. I am not new to boost::thread, as a matter of fact I have used it many, many times in non-qt applications but for some reason I am having issues with this one. Here is the exact code:

header file:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <boost/thread.hpp>

#include <QMainWindow>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
   Q_OBJECT

   public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();

   private:
      Ui::MainWindow *ui;

      static void my_lengthly_method();
};

#endif // MAINWINDOW_H

source file:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
   QMainWindow(parent),
   ui(new Ui::MainWindow)
{
   ui->setupUi(this);

   boost::thread(&my_lengthly_method, this);
}

MainWindow::~MainWindow()
{
   delete ui;
}

void MainWindow::my_lengthly_method()
{

}

.pro file:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

INCLUDEPATH += $$PWD

TARGET = untitled
TEMPLATE = app

LIB_GLOBAL = /usr/lib/x86_64-linux-gnu

DEFINES += QT_DEPRECATED_WARNINGS  

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

LIBS += \
   -L$$LIB_GLOBAL -lboost_system \
   -L$$LIB_GLOBAL -lboost_filesystem \
   -L$$LIB_GLOBAL -lboost_thread \
   -L$$LIB_GLOBAL -lboost_regex

I run the project and:

/usr/include/boost/bind/bind.hpp:259: error: too many arguments to function
         unwrapper<F>::unwrap(f, 0)(a[base_type::a1_]);
         ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~

When you click on the error, it opens up that file and here is what you get: enter image description here

I have used this awesome library in many different non Qt projects before and I have never had any issues. Is there any work around for this?

All of my APIs are based around boost::thread.

I can use Qt threads but I don't want to.

Anyway, right now, I want to get the boost thread thing to work.

标签: c++qtboost

解决方案


my_lengthly_method是静态方法所以this是多余的,只需调用

    boost::thread(&my_lengthly_method);

在上面的行中,您创建了一个临时线程对象,并且在执行此行之后,线程临时对象被销毁,在这个地方您可能会遇到问题,因为在 C++ 标准库中,当调用析构函数std::thread而不调用join它时std::terminate- 您的应用程序已关闭。在 BOOST 中,这取决于您的库是如何构建的,如果使用定义,BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE那么您的代码将起作用。但为了安全起见,您应该命名您的对象并调用deatch方法。

     boost::thread myThread(&my_lengthly_method);
     myThread.detach();
 }

推荐阅读