异步中没有匹配函数(模板中未解析的类型)
no matching function in the async (unresolved type in the template)
我在重用名为 evaluate
的函数时遇到问题
template<typename T>
template<typename T2>
T2 Polynomial<T>::evaluate(T2 val) const {
int degree = 0;
return accumulate(coefs.begin(), coefs.end(), T2{0},
[&](T2 res, T coef){ return res+coef*pow(val,degree++);} );
}
我正在尝试在名为 product
的函数中调用它
template<typename T>
template<typename T2>
T2 Polynomial<T>::product(T2 val, vector<Polynomial<T>>& polys) {
vector<future<T2>> futures;
T2 product = this->evaluate(val);
// evaluate asynchronously each polynomial with val
for (Polynomial<T>& p : polys) {
auto evaluateFunction = [&]() {
int degree = 0;
return accumulate(p.coefs.begin(), p.coefs.end(), T2{0},
//change [&] to [&val, °ree]
[&](T2 res, T coef) { return res + coef * pow(val, degree++); });
};
futures.push_back(async(launch::async, this->evaluate(val))); //normally there was a evaluateFunction there
}
// compute the final product
for (auto& fut: futures) {
product *= fut.get();
}
return product;
}
重用'evaluate'函数报错报错'no matching function'
在异步中(模板中未解析的类型)所以我不得不重新制作它。
我该如何解决这个错误?
我不明白 futures.push_back(async(launch::async, this->evaluate(val)));
有什么问题
this->evaluate(val)
将计算为 this->evaluate(val)
的结果并重新放置
应该在 val
而不是 p
上调用评估
你应该做的
std::bind(&Polynomial<T>::evaluate, p, val)
或使用 lambda
[&]{ return p.evaluate(val); }
这将从 Polynomial<T>::evaluate
创建一个可调用对象,其中 p
和 val
作为参数
我在重用名为 evaluate
template<typename T>
template<typename T2>
T2 Polynomial<T>::evaluate(T2 val) const {
int degree = 0;
return accumulate(coefs.begin(), coefs.end(), T2{0},
[&](T2 res, T coef){ return res+coef*pow(val,degree++);} );
}
我正在尝试在名为 product
template<typename T>
template<typename T2>
T2 Polynomial<T>::product(T2 val, vector<Polynomial<T>>& polys) {
vector<future<T2>> futures;
T2 product = this->evaluate(val);
// evaluate asynchronously each polynomial with val
for (Polynomial<T>& p : polys) {
auto evaluateFunction = [&]() {
int degree = 0;
return accumulate(p.coefs.begin(), p.coefs.end(), T2{0},
//change [&] to [&val, °ree]
[&](T2 res, T coef) { return res + coef * pow(val, degree++); });
};
futures.push_back(async(launch::async, this->evaluate(val))); //normally there was a evaluateFunction there
}
// compute the final product
for (auto& fut: futures) {
product *= fut.get();
}
return product;
}
重用'evaluate'函数报错报错'no matching function' 在异步中(模板中未解析的类型)所以我不得不重新制作它。
我该如何解决这个错误?
我不明白 futures.push_back(async(launch::async, this->evaluate(val)));
this->evaluate(val)
将计算为 this->evaluate(val)
的结果并重新放置
应该在 val
而不是 p
你应该做的
std::bind(&Polynomial<T>::evaluate, p, val)
或使用 lambda
[&]{ return p.evaluate(val); }
这将从 Polynomial<T>::evaluate
创建一个可调用对象,其中 p
和 val
作为参数