【问题标题】:Vector of buffers in C++C++ 中的缓冲区向量
【发布时间】:2016-12-14 16:57:17
【问题描述】:

我创建了这个向量,它有缓冲区:

std::vector<std::unique_ptr<locked_buffer<std::pair<int, std::vector<std::vector<unsigned char>>>>>> v1;

然后,我用n 缓冲区填充这个向量,这个缓冲区有aux 元素。 n 是一个 int 数字,它是一个参数。 aux也是一个int类型的另一个参数。

for(int i=0; i<n; i++){
   v1.push_back(std::unique_ptr<locked_buffer<std::pair<int,std::vector<std::vector<unsigned char>>>>> (new locked_buffer<std::pair<int, std::vector<std::vector<unsigned char>>>>(aux)));  
}

但是当我尝试访问向量的每个缓冲区时却无法访问,因为我不清楚如何访问向量结构的特定元素:

v1[0].put(image, false);

我遇到的编译错误如下(put 函数是在我的自定义locked_buffer 结构上定义的):

error: ‘_gnu_cxx::_alloc_traits<std::allocator<std::unique_ptr<locked_buffer<std::pair<int, std::vector<std::vector<unsigned char> > > > > > >::value_type {aka class std::unique_ptr<locked_buffer<std::pair<int, std::vector<std::vector<unsigned char> > > > >}’ has no member named ‘put’
v1[i].put(image, false);

谢谢。

【问题讨论】:

  • 我认为您需要取消引用unique_ptrv1[i]-&gt;put... 也是如此
  • 最后的双向量是否应该是二维矩阵?此外,这将是使用using 的好地方。比如template&lt;class T&gt; using myBufferType = std::pair&lt;int, std::vector&lt;std::vector&lt;T&gt;&gt;&gt;; 那么你的类型就是std::unique_ptr&lt;locked_buffer&lt;myBufferType&lt;unsigned char&gt;&gt;&gt;

标签: c++ vector buffer stdvector unique-ptr


【解决方案1】:

v1[0]unique_ptr&lt;locked_buffer&lt;...&gt;&gt;。为了在包含的locked_buffer 上调用方法,您需要取消引用unique_ptr,即

(*v1[0]).put(image, false);

v1[0]->put(image, false);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 2020-05-09
    • 2014-10-30
    • 1970-01-01
    • 2011-11-14
    相关资源
    最近更新 更多