Mal*_*mir 2 c++ shared-ptr c++11
CustomDynamicArray类包装std::vector并通过重载运算符通过索引对提供对其项目的访问operator()
CustomCell& CustomDynamicArray::operator()(size_t colIdx, size_t rowIdx)
Run Code Online (Sandbox Code Playgroud)
它使得以自然的方式寻址数组项成为可能:
CustomDynamicArray _field;
_field(x, y) = cell;
const CustomCell& currentCell = _field(x, y);
Run Code Online (Sandbox Code Playgroud)
但由于我将变量覆盖到std::shared_ptr我有一个错误
std::shared_ptr<CustomDynamicArray> _fieldPtr;
_fieldPtr(x, y) = cell; // Error! C2064 term does not evaluate to a function taking 2 arguments
const CustomCell& currentCell = _fieldPtr(x, y); // Error! C2064 term does not evaluate to a function taking 2 arguments
Run Code Online (Sandbox Code Playgroud)
我该如何修复这个编译错误?现在我只能看到使用此语法的方法:
(*_newCells)(x, y) = cell;
Run Code Online (Sandbox Code Playgroud)
std::shared_ptr是智能指针,其行为类似于原始指针,您不能operator()像那样直接调用指针。您可以取消引用std::shared_ptr然后调用operator().
(*_fieldPtr)(x, y) = cell;
const CustomCell& currentCell = (*_fieldPtr)(x, y);
Run Code Online (Sandbox Code Playgroud)
或者operator()显式调用(以丑陋的方式)。
_fieldPtr->operator()(x, y) = cell;
const CustomCell& currentCell = _fieldPtr->operator()(x, y);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1482 次 |
| 最近记录: |