c++ - 使用 boost::iterator_adaptor 的无信息错误

标签 c++ generics boost iterator iostream

我正在尝试使用 boost::iterator_adaptorboostprogress_display 包装一个 istream。我写的是

class ifstreamWithProgress: public boost::iterator_adaptor <
  ifstreamWithProgress,
  char*,
  boost::use_default,
    boost::forward_traversal_tag > {
public:
//  ifstreamWithProgress(const char* fname_) {}
  ifstreamWithProgress(): iter(std::istream_iterator<char>()), pd(0)
  {}

  ifstreamWithProgress(const std::string& fname_): fname(fname_), fsize(0), pd(fsize) {
    std::ifstream file(fname.c_str(), std::ios::binary);
    fsize = file.tellg();
    file.seekg(0, std::ios::end);
    fsize = file.tellg() -  fsize;
    file.seekg(0, std::ios::beg);
    iter = std::istream_iterator<char>(file);
    pd.restart(fsize);
  }

  ~ifstreamWithProgress() {
    while( ++pd < fsize);
  }

  const std::istream_iterator<char> getRawIstream() const {
    return iter;
  }

private:
  std::string fname;
  friend class boost::iterator_core_access;
  std::istream_iterator<char> iter;
  std::streampos fsize;
  progress_display pd;

  void increments() {
    iter++;
    ++pd;
  }

  bool equal(const ifstreamWithProgress& rhs) const {
    return this->iter == rhs.getRawIstream();
  }
};

这编译。但是,当我开始做类似

的事情时
  ifstreamWithProgress is("data.txt");
  ifstreamWithProgress eos;
  is != eos;

我收到一个编译时错误,指出它不可复制。这是有道理的,因为显示类是从 boost::noncopyable 派生的。但是我没有得到的是复制发生的地方。有什么指点吗?

PS:报错信息是

1>c:\users\leon.sit\documents\myprojects\c++\general_models\phoenixdsm\phx\fileProgressBarWrapper.hpp(58) : error C2248: 'boost::noncopyable_::noncopyable::noncopyable' : cannot access private member declared in class 'boost::noncopyable_::noncopyable'
1>        C:\Program Files\boost\boost_1_44\boost/noncopyable.hpp(27) : see declaration of 'boost::noncopyable_::noncopyable::noncopyable'
1>        C:\Program Files\boost\boost_1_44\boost/noncopyable.hpp(22) : see declaration of 'boost::noncopyable_::noncopyable'
1>        This diagnostic occurred in the compiler generated function 'ifstreamWithProgress::ifstreamWithProgress(const ifstreamWithProgress &)'

它不指向源代码中的任何地方。但是它在注释比较行之后编译。

最佳答案

不必在任何地方进行复制都会产生此错误。这是编译器必须生成复制构造函数这一事实的简单反射(reflect)。但要做到这一点,它必须复制 progress_display同样,这是不可能的,因为后者的复制构造函数是私有(private)的。

您可以通过声明指向 progress_display 的指针来变通成员并定义您自己的复制构造函数和 = 运算符。例如:

class ifstreamWithProgress: public boost::iterator_adaptor <
  ifstreamWithProgress,
  char*,
  boost::use_default,
    boost::forward_traversal_tag > {
public:
  ifstreamWithProgress() : pd(0) {
    pd = new progress_display(...);
  }

  ifstreamWithProgress::ifstreamWithProgress(const ifstreamWithProgress &r) : pd(0) {
    pd = new progress_display(...);
  }

  ~ifstreamWithProgress() {
    if (0 != pd) {
      delete pd;
    }
  }

  ifstreamWithProgress& operator= (const ifstreamWithProgress &r) {
    if (0 != pd) {
      delete pd;
    }
    pd = new progress_display(...);
    return *this;
  }

private:
  progress_display *pd;
};

或者您可以使用 shared_ptr<progress_display> .

关于c++ - 使用 boost::iterator_adaptor 的无信息错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4436107/

相关文章:

c++ - 神秘的编译器错误,可能与使用 "virtual"有关?

Java 泛型 : using parameterized interface in method calls

c++ - CLion 和 Boost 1.60.0

typescript - 如何在 typescript 中为带有参数但没有任何参数的函数类型指定类型保护?

c++ - Boost线程等待条件

c++ - 使用 cpprestsdk 和 boost 固定证书

c++ - 当父类共享指针为返回类型时返回新的基类

C++ 精度 : String to Double

c++ - 使用 stringstream 和 if-else 语句时的字符串输入错误

java - 为什么 KClass 声明为 KClass<T : Any> and not KClass<T> so that the type argument can be nullable