[英]Shared pointer dereferencing error
Java guy here trying to get my head around C++, specifically shared pointers . 这里的Java专家试图使我了解C ++,特别是共享指针 。 I'm using the Point Cloud Library to do some surface work.
我正在使用点云库来做一些表面工作。 The PCL library contains
IndicesPtr
which according to the documentation is a shared pointer on a std::vector
. PCL库包含
IndicesPtr
,根据文档, IndicesPtr
是std::vector
上的共享指针 。
How do I access the vector from the pointer? 如何从指针访问向量? I have tried dereferencing with
我尝试与
pcl::IndicesPtr sample(new std::vector<int>());
...
for (int i = 0; i < *sample.size(); i++) { ... }
as per the documentation here https://theboostcpplibraries.com/boost.smartpointers-shared-ownership . 根据此处的文档https://theboostcpplibraries.com/boost.smartpointers-shared-ownership 。 Compiling then gives me the error
编译然后给我错误
error: no member named 'size' in 'boost::shared_ptr<std::__1::vector<int, std::__1::allocator<int> > >'; did you mean to use '->' instead of '.'?
for (int i = 0; i < *sample.size(); i++) {
What am I doing wrong here? 我在这里做错了什么?
It should either be (*sample).size()
because operator .
它应该是
(*sample).size()
因为operator .
has higher precedence over dereference operator *
or just sample->size()
. 比取消引用
operator *
或仅具有sample->size()
优先级更高。
According to operator precedence , operator.
根据运算符优先级 ,
operator.
has higher precedence than operator*
. 具有比
operator*
高的优先级。 So *sample.size()
is same as *(sample.size())
. 因此
*sample.size()
与*(sample.size())
。 That's why the compiler tried to tell you that you can't invoke size()
on a boost::shared_ptr
directly. 这就是为什么编译器试图告诉您不能直接在
boost::shared_ptr
上调用size()
。
You can add parentheses to specify precedence explicitly, eg (*sample).size()
; 您可以添加括号以明确指定优先级,例如
(*sample).size()
; or as the compiler suggested, change it to sample->size()
. 或者按照编译器的建议,将其更改为
sample->size()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.