Welcome in the forum!
You need to remove the "Hardware Connection" in the pin's property dialog. Then you may control the pin by PinName_Write() and PinName_Read().
I have seen the same error occur even when the "Hardware Connection" option is unchecked when the pin is defined as an input. Redefining the pin as an output causes the error to disappear.