我是 boost c++ 库的新手,我有以下问题。
我的程序中有以下代码片段。
typedef std::vector<int> Indices;
typedef boost::shared_ptr<Indices> IndicesPtr;
...
IndicesPtr indices (new Indices);
在我将数据添加到 indices 之后,我想使用 operator [] 访问单个成员,但我收到一条错误消息。
错误:错误:indices[i] 中的 operator[] 不匹配。我不明白为什么会这样。使用 boost::shared_ptr<> 时是否有特定的访问方式。
最佳答案
你不应该在这里使用shared_ptr
,只使用vector
,但如果你想(例如训练)你可以使用
indices->operator[](index)
或取消引用 shared_ptr
(*indices)[index]
就像原始指针一样。
关于c++ - boost shared_ptr vector 成员访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31877563/