【发布时间】:2020-06-03 23:00:19
【问题描述】:
所以我一直在尝试使用变换函数从 2 个现有的向量中创建一个向量。我成功地设法在没有类的情况下做到了,但现在我想使用类,但我得到了错误。下面的代码只是我项目的一小部分相关部分,所以它可能看起来很傻,但整个事情最终实际上是有意义的。
这是我的头文件的一部分:
#include <vector>
#include <algorithm>
class Example
{
public:
double multiply(double x, double y);
void generate_amplitude(std::vector<double>& ampVec);
};
还有我的 .cpp 文件的一些内容:
#include "example.h"
double Example::multiply(double x, double y)
{
return x*y;
}
void Example::generate_amplitude(std::vector<double>& ampVec)
{
double const a = 3.14;
int const lenVectors = 10;
std::vector<double> timeVec = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}
std::vector<double> ampVec(lenVectors);
std::vector<double> aVec(lenVectors, a);
// Fill ampVec with timeVec[i]*aVec[i] for i in range[0:lenVectors[
transform(timeVec.begin(), timeVec.end(), aVec.begin(), ampVec.begin(), multipy);
}
所以当我尝试编译时,我得到:
error: must use '.*' or '->*' to call pointer-to-member function in '__binary_op (...)', e.g. '(... ->* __binary_op) (...)'
我确实搜索并阅读了几个小时关于指向成员函数的内容,尝试了几种不同的方法,例如:
double (Example::*pmf)(double, double);
pmf = &Example::multiply;
transform(timeVec.begin(), timeVec.end(), aVec.begin(), ampVec.begin(), this->*pmf);
但我还是新手,无法找到让它发挥作用的方法。这个特殊的例子返回了那种消息:
error: no matching function for call to 'transform(std::vector<double>::iterator, std::vector<double>::iterator, std::vector<double>::iterator, std::vector<double>::iterator, double (Example::)(double, double))'
note: candidate: 'template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)'
note: candidate expects 4 arguments, 5 provided
note: template argument deduction/substitution failed
note: candidate: 'template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)'
note: member function type 'double (Example::)(double, double)' is not a valid template argument
非常感谢您的帮助!
【问题讨论】:
标签: c++ vector transform pointer-to-member