0

I would like to have a constructor of class to have a lambda as a parameter:

class MyComboBox : public QComboBox
{
public:
 explicit MyComboBox( const std::function< QString( Flag ) > &readable, QWidget *parent = nullptr );
};

I have some flags in a class, which also have a static method:

class QgsField
{
  Q_GADGET
  public:
    enum class ConfigurationFlag : int
    {
      Searchable = 1 << 1,
      Readable = 1 << 2,
    };
    Q_ENUM( ConfigurationFlag )
    Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
    Q_FLAG( ConfigurationFlags )

    static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag );
};

I am trying to construct MyComboBox:

std::function<QString (QgsField::ConfigurationFlag)> readable = [](QgsField::ConfigurationFlag flag) {return QgsField::readableConfigurationFlag(flag);};
QComboBox *cb = new MyComboBox<QgsField::ConfigurationFlag>(readable, parent );

This fails:

Undefined symbols for architecture x86_64: "QgsFlagCheckableComboBoxQgsField::ConfigurationFlag::QgsFlagCheckableComboBox(std::__1::function<QString (QgsField::ConfigurationFlag)>&, QWidget*)

  1. Is there a way to pass the static method directly without going through a lambda (by definition the base class can change)?
  2. Why is it currently failing?
Denis Rouzaud
  • 2,412
  • 2
  • 26
  • 45
  • 2
    Taking `std::function` by (non const) reference is strange (and disallow to take temporary one such as one created by lambda), by value or by const reference or even by r-value reference seems better. – Jarod42 Sep 15 '20 at 12:53
  • 1
    you say lambda but then the constructor takes a `std::function`. It is a common misunderstanding that you need to use `std::function` to pass functions as parameter or that it would be good to do so per se, both isnt quite true – 463035818_is_not_an_ai Sep 15 '20 at 13:04
  • 2
    `explicit QgsFlagCheckableComboBox` looks like a constructor but it is not. – Christopher Yeleighton Sep 15 '20 at 13:04
  • @ChristopherYeleighton why is it not? what should it be? – Denis Rouzaud Sep 15 '20 at 14:12
  • The name of the constructor must be the same as the name of the class. – Christopher Yeleighton Sep 15 '20 at 14:13
  • oops ;) I fixed this and switched to a const ref. Thanks for the pointer, but the problems stays... – Denis Rouzaud Sep 15 '20 at 14:24
  • @idclev463035818 di you have any reference to some documentation or can explain a bit more? – Denis Rouzaud Sep 15 '20 at 15:01
  • @Denis Rouzaud The Flag type and the QgsField::ConfigurationFlag type are the same types? Can you compile this code?: `std::function readable = [](QgsField::ConfigurationFlag flag) {return QgsField::readableConfigurationFlag(flag);}; const std::function< QString( Flag ) > &readable2 = readable;` – B0FEE664 Sep 15 '20 at 15:06

0 Answers0