在 C++ 中将方法放入缓冲区

Put method into a buffer in C++

我创建了这个方法来将一些数据放入缓冲区:

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();
}

但是,尝试将包含在函数 return 中的数据放入:

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)'

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

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

  1. 您的 locked_buffer 对象模板化为类型:long int
  2. 您的第 1st 个参数的类型为: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