error: invalid use of incomplete type InvalidConversion
InvalidConversion
is a dummy class: it’s declared but not defined.
ArduinoJson uses this class to trigger a recognizable compilation error.
This error occurs when you try to get a mutable reference from a read-only reference.
For example, the following function receives a const
reference to a JsonDocument
, so it cannot get a JsonArray
from it:
void printPorts(const JsonDocument& doc) {
JsonArray ports = doc["ports"]; // <-- error here
for (int port : ports)
Serial.println(port);
}
If you try to compile this code, you’ll get an error like this one:
In file included from .../ArduinoJson/Variant/VariantRefBase.hpp:9:0,
from .../ArduinoJson/Array/ElementProxy.hpp:7,
from .../ArduinoJson/Array/JsonArray.hpp:7,
from .../ArduinoJson.hpp:29,
from .../ArduinoJson.h:9,
from MyProject.ino:1:
.../ArduinoJson/Variant/JsonVariantConst.hpp: In instantiation of 'typename ArduinoJson::V701L1::detail::enable_if<((! ArduinoJson::V701L1::detail::is_same<T, char*>::value) && (! ArduinoJson::V701L1::detail::is_same<T, char>::value)), T>::type ArduinoJson::V701L1::JsonVariantConst::as() const [with T = ArduinoJson::V701L1::JsonArray; typename ArduinoJson::V701L1::detail::enable_if<((! ArduinoJson::V701L1::detail::is_same<T, char*>::value) && (! ArduinoJson::V701L1::detail::is_same<T, char>::value)), T>::type = ArduinoJson::V701L1::JsonArray]':
.../ArduinoJson/Variant/JsonVariantConst.hpp:84:17: required from 'ArduinoJson::V701L1::JsonVariantConst::operator T() const [with T = ArduinoJson::V701L1::JsonArray]'
MyProject.ino:4:32: required from here
.../ArduinoJson/Variant/JsonVariantConst.hpp:69:34: error: invalid use of incomplete type 'class ArduinoJson::V701L1::detail::InvalidConversion<ArduinoJson::V701L1::JsonVariantConst, ArduinoJson::V701L1::JsonArray>'
return Converter<T>::fromJson(*this);
~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
In file included from .../ArduinoJson/Variant/VariantRefBase.hpp:8:0,
from .../ArduinoJson/Array/ElementProxy.hpp:7,
from .../ArduinoJson/Array/JsonArray.hpp:7,
from .../ArduinoJson.hpp:29,
from .../ArduinoJson.h:9,
from MyProject.ino:1:
.../ArduinoJson/Variant/Converter.hpp:20:7: note: declaration of 'class ArduinoJson::V701L1::detail::InvalidConversion<ArduinoJson::V701L1::JsonVariantConst, ArduinoJson::V701L1::JsonArray>'
class InvalidConversion; // Error here? See https://arduinojson.org/v7/invalid-conversion/
^~~~~~~~~~~~~~~~~
After removing the namespaces and all the remaining noise, you get this:
invalid use of incomplete type 'class InvalidConversion<JsonVariantConst, JsonArray>'
As you can see, the error message is quite explicit: you cannot convert a JsonVariantConst
to JsonArray
. Indeed, the const
version of JsonDocument::operator[]
returns a JsonVariantConst
, which is read-only, so you cannot convert it to a mutable reference like JsonArray
.
The solution is to replace the mutable reference type with the read-only equivalent.
In this case, we must replace JsonArray
with JsonArrayConst
:
void printPorts(const JsonDocument& doc) {
- JsonArray ports = doc["ports"];
+ JsonArrayConst ports = doc["ports"];
for (int port : ports)
Serial.println(port);
}