c++ - 可以将 boost 累加器用作类成员

标签 c++ boost boost-accumulators

我正在尝试使用 boost 累加器来计算滚动平均值。当我这样声明内联变量时:

#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>

using namespace boost::accumulators;

int main()
{
  // Define rolling_mean accumulator
  accumulator_set<double, stats<tag::rolling_mean > > acc(tag::rolling_window::window_size = 5);
    // push in some data ...
    acc(1.2);
    acc(2.3);
    acc(3.4);
    acc(4.5);

    // Display the results ...
    std::cout << "Mean:   " << rolling_mean(acc) << std::endl;

    return 0;
}

它工作得很好。当我像这样将累加器声明为类的成员时:

#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>

using namespace boost::accumulators;

class DoMean {
private:
  accumulator_set<double, stats<tag::rolling_mean > > m_acc(tag::rolling_window::window_size = 5);

public:

  void addData(double val) {
    this->m_acc(val);
  }

  double getMean(void) {
    return rolling_mean(this->m_acc);
  }
};

int main()
{
  // Define an accumulator set for calculating the mean and the
  // 2nd moment ...
  DoMean meaner;
  meaner.addData(1.2);
  meaner.addData(2.3);
  meaner.addData(3.4);
  meaner.addData(4.5);

  // push in some data ...

  // Display the results ...
  std::cout << "Mean:   " << meaner.getMean() << std::endl;

  return 0;
}

它失败了,给了编译器错误:

accumulators::tag::rolling_window::window_size is not a type
...blah blah, many type template errors etc.

最佳答案

这个问题的正确解法是这样的:

#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>

using namespace boost::accumulators;

class DoMean {
private:
  accumulator_set<double, stats<tag::rolling_mean > > m_acc;

public:

  DoMean(void): m_acc(tag::rolling_window::window_size = 5) {}

  void addData(double val) {
    this->m_acc(val);
  }

  double getMean(void) {
    return rolling_mean(this->m_acc);
  }
};

int main()
{
  // Define an accumulator set for calculating the mean and the
  // 2nd moment ...
  DoMean meaner;
  meaner.addData(1.2);
  meaner.addData(2.3);
  meaner.addData(3.4);
  meaner.addData(4.5);

  // push in some data ...

  // Display the results ...
  std::cout << "Mean:   " << meaner.getMean() << std::endl;

  return 0;
}

请注意,m_acc 的初始化已从与其声明内联移动到初始化列表中。这解决了所有编译器错误。事实上,如果我们考虑这里发生的事情,最初尝试在类中使用累加器失败的原因是因为 ISO c++ 禁止内联成员初始化。

我们可以用另一个简单的类来证明这一点:

#include <iostream>

class TestInit {
public:
  int m_init = 10;

};

int main() {
  TestInit inits;
  std::cout << "The value: " << inits.m_init << std::endl;

}

现在编译器给了我们一条有用的信息:

/home/me/prog/cpp/acctest/testinit.cxx:5:16: error: ISO C++ forbids initialization of member m_init [-fpermissive]
/home/me/prog/cpp/acctest/testinit.cxx:5:16: error: making m_init static [-fpermissive]
/home/me/prog/cpp/acctest/testinit.cxx:5:16: error: ISO C++ forbids in-class initialization of non-const static member m_init

关于c++ - 可以将 boost 累加器用作类成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15237135/

相关文章:

c++ - 使用 boost serialize 的 Microsoft GUID 序列化?

c++ - boost 偏度 C++ 示例

c++ - 总结两个 boost::accumulator_set 实例

c++ - 为什么我不能用 c = new (char*)[10] 初始化 char** c,而 c = new char*[10] 可以?

c++ - 指向接受函数指针的函数的函数指针

c++ - 在为请求选择服务的情况下,使用 Boost ICL 而不是 for_each 是否可能且合理?

python - 在 C++ 中运行 python

c++ - boost 累加器滚动计数非零

c++ - 从 OpenCV FileStorage 读取会导致运行时崩溃

c++ - 在另一个类中使用一个类的静态对象