1

I have my own extended 2d-array class CustomDynamicArray which cover std::vector and allow handle its items by indexes via overloaded operator()

CustomCell& CustomDynamicArray::operator()(size_t colIdx, size_t rowIdx)

until I had simple CustomDynamicArray instance

CustomDynamicArray _field;

I might adressed to array items next way:

_field(x, y) = cell;

or

const CustomCell& currentCell = _field(x, y);

But since I covered my varrible into std::shared_ptr I have an error

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

How I should fix this compile error?

Now I see only way to use this syntax:

(*_newCells)(x, y) = cell;
Malov Vladimir
  • 415
  • 4
  • 9

2 Answers2

4

std::shared_ptr is smart pointer which behaves like raw pointers, you can't call operator() on the pointer directly like that. You could dereference on the std::shared_ptr then call the operator().

(*_fieldPtr)(x, y) = cell;
const CustomCell& currentCell = (*_fieldPtr)(x, y);

Or invoke operator() explicitly (in ugly style).

_fieldPtr->operator()(x, y) = cell;
const CustomCell& currentCell = _fieldPtr->operator()(x, y);
songyuanyao
  • 147,421
  • 15
  • 261
  • 354
1

You have to deref the pointer first:

(*_fieldPtr)(x, y) = cell;
const CustomCell& currentCell = (*_fieldPtr)(x, y);
Hatted Rooster
  • 33,170
  • 5
  • 52
  • 104