C ++,没有用于调用“过滤器”的匹配函数

问题描述 投票:1回答:1

我写了一个名为Matrix的类,下面的代码有效

Matrix Matrix::operator<(int num) const {
    Matrix tmp=*this;
    Between t(1,2);
    return filter(*this,t);
}

但是为什么它不能编译?

Matrix Matrix::operator<(int num) const {
    Matrix tmp=*this;
    return filter(*this,Between(1,2););
}

以及如何解决此问题?

Matrix filter (const Matrix& int_matrix, Between& field)
c++ class
1个回答
1
投票

[filter通过左值引用第二个参数来引用非常量,该常量不能绑定到像Between(1,2)这样的临时对象。

作为解决方法,您可以使filter通过左值引用对const进行参数传递,这可以绑定到临时对象。

Matrix filter (const Matrix& int_matrix, const Between& field)

或传递值。

Matrix filter (const Matrix& int_matrix, Between field)
© www.soinside.com 2019 - 2024. All rights reserved.