no matching function for call to ‘BasicJsonDocument::BasicJsonDocument()’
(applies to ArduinoJson 6.10 and later)
How do we get this error?
If you try to compile the following program:
DynamicJsonDocument doc;
you’ll get this horrible error:
MyProgram.ino: In function 'void setup()':
MyProgram:21:23: error: no matching function for call to 'ArduinoJson6130_0000010::BasicJsonDocument<ArduinoJson6130_0000010::DefaultAllocator>::BasicJsonDocument()'
DynamicJsonDocument doc;
^~~
In file included from ArduinoJson/Document/DynamicJsonDocument.hpp:7:0,
from ArduinoJson.hpp:21,
from ArduinoJson.h:9,
from MyProgram.ino:9:
ArduinoJson/Document/BasicJsonDocument.hpp:56:3: note: candidate: ArduinoJson6130_0000010::BasicJsonDocument<TAllocator>::BasicJsonDocument(ArduinoJson6130_0000010::JsonVariant) [with TAllocator = ArduinoJson6130_0000010::DefaultAllocator]
BasicJsonDocument(JsonVariant src)
^~~~~~~~~~~~~~~~~
ArduinoJson/Document/BasicJsonDocument.hpp:56:3: note: candidate expects 1 argument, 0 provided
ArduinoJson/Document/BasicJsonDocument.hpp:49:3: note: candidate: template<class T> ArduinoJson6130_0000010::BasicJsonDocument<TAllocator>::BasicJsonDocument(const T&, typename ArduinoJson6130_0000010::enable_if<ArduinoJson6130_0000010::IsVisitable<T>::value>::type*)
BasicJsonDocument(const T& src,
^~~~~~~~~~~~~~~~~
ArduinoJson/Document/BasicJsonDocument.hpp:49:3: note: template argument deduction/substitution failed:
MyProgram.ino:21:23: note: candidate expects 2 arguments, 0 provided
DynamicJsonDocument doc;
^~~
In file included from ArduinoJson/Document/DynamicJsonDocument.hpp:7:0,
from ArduinoJson.hpp:21,
from ArduinoJson.h:9,
from MyProgram.ino:9:
ArduinoJson/Document/BasicJsonDocument.hpp:42:3: note: candidate: ArduinoJson6130_0000010::BasicJsonDocument<TAllocator>::BasicJsonDocument(const ArduinoJson6130_0000010::BasicJsonDocument<TAllocator>&) [with TAllocator = ArduinoJson6130_0000010::DefaultAllocator]
BasicJsonDocument(const BasicJsonDocument& src)
^~~~~~~~~~~~~~~~~
ArduinoJson/Document/BasicJsonDocument.hpp:42:3: note: candidate expects 1 argument, 0 provided
ArduinoJson/Document/BasicJsonDocument.hpp:39:12: note: candidate: ArduinoJson6130_0000010::BasicJsonDocument<TAllocator>::BasicJsonDocument(size_t, TAllocator) [with TAllocator = ArduinoJson6130_0000010::DefaultAllocator; size_t = unsigned int]
explicit BasicJsonDocument(size_t capa, TAllocator allocator = TAllocator())
^~~~~~~~~~~~~~~~~
ArduinoJson/Document/BasicJsonDocument.hpp:39:12: note: candidate expects 2 arguments, 0 provided
Understanding the error
Extract the signal from the noise
It’s a long output, but it’s not that complicated. Once you remove all the noise (namespaces, template parameters, file paths…), it’s just:
error: no matching function for call to 'BasicJsonDocument::BasicJsonDocument()'
DynamicJsonDocument doc;
^~~
note: candidate: BasicJsonDocument::BasicJsonDocument(JsonVariant)
BasicJsonDocument(JsonVariant src)
^~~~~~~~~~~~~~~~~
note: candidate expects 1 argument, 0 provided
note: candidate: template<class T> BasicJsonDocument::BasicJsonDocument(const T&, typename enable_if<IsVisitable<T>::value>::type*)
BasicJsonDocument(const T& src,
^~~~~~~~~~~~~~~~~
note: template argument deduction/substitution failed:
note: candidate expects 2 arguments, 0 provided
note: candidate: BasicJsonDocument::BasicJsonDocument(const BasicJsonDocument&)
BasicJsonDocument(const BasicJsonDocument& src)
^~~~~~~~~~~~~~~~~
note: candidate expects 1 argument, 0 provided
note: candidate: BasicJsonDocument::BasicJsonDocument(size_t, TAllocator)
explicit BasicJsonDocument(size_t capa, TAllocator allocator = TAllocator())
^~~~~~~~~~~~~~~~~
note: candidate expects 2 arguments, 0 provided
What does it mean?
What the compiler is trying to say is this: when compiling DynamicJsonDocument doc;
, I’ve been looking for BasicJsonDocument::BasicJsonDocument()
but I didn’t find it. Instead, I found:
BasicJsonDocument(JsonVariant src)
BasicJsonDocument(const T&, typename enable_if<IsVisitable<T>::value>::type*)
BasicJsonDocument(const BasicJsonDocument& src)
BasicJsonDocument(size_t capa, TAllocator allocator = TAllocator())
So, the compiler tried to find a constructor with no parameter, but it didn’t find one. This means we forgot to pass an argument to the constructor.
How does this relate to DynamicJsonDocument
?
DynamicJsonDocument
is an alias of BasicJsonDocument
; it’s defined like that:
typedef BasicJsonDocument<DefaultAllocator> DynamicJsonDocument;
That’s why the compiler says BasicJsonDocument
when we use DynamicJsonDocument
How to solve the error?
To fix this error, you must pass an argument to the constructor of DynamicJsonDocument
.
It’s an integer that specifies the capacity of the memory pool; it’s a mandatory argument in ArduinoJson 6.
DynamicJsonDocument doc(capacity);
To choose the right capacity for your project, use the ArduinoJson Assistant, or read How to determine the capacity of the JsonDocument?