【问题标题】:Error while trying to use the boost::variant - "No matching function for call"尝试使用 boost::variant 时出错 - “没有匹配的调用函数”
【发布时间】:2021-12-27 17:20:22
【问题描述】:

在使用 boost::variant 时,我不断收到错误提示“没有匹配的函数调用”。 下面是我的代码 sn-p。

struct Output {
    int a;
    float b;
}
    
typedef boost::variant<ClassA<X, Y>, ClassA<>> ClassAGeneric;

class Operation: public boost::static_visitor<Output>
{
public:
    double d;
    int a;
    float b;
    
    Output operator()(ClassA<X, Y> obj) const
    {
        obj.operate(d, a, b);
        return (Output) {a, b};
    }
    
    Output operator()(ClassA<> obj) const
    {
        obj.operate(d, a, b);
        return (Output) {a, b};
    }
};

我在定义的 first operator() 中的 obj.operate() 调用中遇到此错误。

我尝试过传递模板,就像另一个答案中提到的那样,但我仍然看到一个错误。

obj.operate<X,Y>(d,a,b);

有人可以帮我解决这个问题吗?

我也可以在这里给出确切的场景:

struct Output{
  Row<size_t> predictions;
  mat probabilities;
};
typedef boost::variant<RandomForest<GiniGain, RandomDimensionSelect>, RandomForest<>> RandomForestGeneric;

class Operation: public boost::static_visitor<Output>
{
public:
    mat dataset;
    Row<size_t> predictions;
    mat probabilities;
    
    Output operator()(RandomForest<GiniGain, RandomDimensionSelect> obj) const
    {
        obj.Classify(dataset, predictions, probabilities);
        return (Output) {predictions, probabilities};
    }
    
    Output operator()(RandomForest<> obj) const
    {
        obj.Classify(dataset, predictions, probabilities);
        return (Output) {predictions, probabilities};
    }
};

【问题讨论】:

标签: c++ boost variant boost-variant mlpack


【解决方案1】:

这是我想象中的独立测试器:

Live On Coliru

#include <boost/variant.hpp>
#include <iostream>

struct X;
struct Y;
template <typename... T> struct ClassA {
    void operate(double d, int a, float b) const
    {
        std::cout << __PRETTY_FUNCTION__ << "(" << d << "," << a << "," << b << ")\n";
    }
};

struct Output {
    int   a;
    float b;
};

typedef boost::variant<ClassA<X, Y>, ClassA<>> ClassAGeneric;

class Operation // : public boost::static_visitor<Output>
{
  public:
    double d;
    int    a;
    float  b;

    Output operator()(ClassA<X, Y> const& obj) const
    {
        obj.operate(d, a, b);
        return Output{a, b};
    }

    Output operator()(ClassA<> const& obj) const
    {
        obj.operate(d, a, b);
        return Output{a, b};
    }
};

int main() {
    Operation op {3.14, 42, 9e-2f};


    ClassAGeneric v1 = ClassA<X,Y>{};
    ClassAGeneric v2 = ClassA<>{};
    apply_visitor(op, v1);
    apply_visitor(op, v2);
}

打印

void ClassA::operate(double, int, float) const with T = {X, Y}
void ClassA::operate(double, int, float) const with T = {}

不出所料,这很有效。现在,一个陷阱可能是您未能创建 operate 成员函数 const 并且参数实际上是 const

还请注意,您可以大大简化访问者(尤其是假设 C++14):Live On Coliru

【讨论】:

  • 感谢您的回复,但在我的用例中尝试这样做对我不起作用。我正在尝试使用带有和不带有超参数的 mlpack 的 RandomForest。所以 typedef 如下所示 - typedef boost::variant, RandomForest> RandomForestGeneric;并且操作是在mlpack中定义的RandomForest类的成员函数 - Classify()
  • 这就是为什么您需要在问题中包含自包含代码的原因。我建议你看看我提出的关于 const 成员的建议。
  • 考虑到 RandomForest 听起来可能无法复制或复制成本高昂这一事实,请考虑通过引用传递它们(updated my answer code 以反映该更改)。
  • 我尝试了同样的方法,但仍然出现同样的错误。错误:没有匹配函数调用 'mlpack::tree::RandomForest<:tree::ginigain mlpack::tree::randomdimensionselect>::Classify(const mat&, const arma::Row &, const mat&) const' 从文档中我看到只有一个参数(第一个)是 const。我不确定为什么所有参数都被转换为 const。 mlpack.org/doc/mlpack-git/doxygen/…
  • 你为什么假设“所有的参数都被转换为 const”?这似乎是一个大胆的假设。同样,如果没有代码甚至完整的错误消息,您只是在盲目地(错误地)解释情况并希望我们神奇地知道您做错了什么。祝你好运。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
相关资源
最近更新 更多