将方法放入C 中的缓冲液中

Put method into a buffer in C++

本文关键字:缓冲 方法      更新时间:2023-10-16

我创建了此方法将一些数据放在缓冲区中:

template <typename T>
void locked_buffer<T>::put(const T & x, bool last) noexcept
{
  using namespace std;
  unique_lock<mutex> l{mut_};
  not_full_.wait(l, [this] { return !do_full(); });
  buf_[next_write_] = item{last,x};
  next_write_ = next_position(next_write_);
  l.unlock();
  not_empty_.notify_one();
}

但是,试图放置函数返回的数据:

int size_b;
locked_buffer<long buf1{size_b}>;
buf1.put(image, true); //image is the return of the function

我对布尔变量bool last有问题,因为我有编译错误。

谢谢。

编辑:我获得的错误是以下一个:

error: no matching function for call to 'locked_buffer<long int>::put(std::vector<std::vector<unsigned char>>&, bool)'

错误:呼叫locked_buffer<long int>::put(std::vector<std::vector<unsigned char>>&, bool)

没有匹配功能

告诉您您需要知道的一切:

  1. 您的locked_buffer对象被模板为类型:long int
  2. 您的1 st 参数是类型: vector<vector<unsigned char>>

现在,我们知道这两种类型必须从您的函数定义中相同:

template <typename T>
void locked_buffer<T>::put(const T & x, bool last) noexcept

编译器的错误是正确的。您需要使用匹配的locked_buffer对象或创建新功能:

template <typename T, typename R>
void locked_buffer<T>::put(const R& x, bool last) noexcept