0

My sketch contains 2 .h files. one defines

on 1st .h file:

#define JSON_SIZE_IOT 400
#define JSON_SIZE_SKETCH 300
StaticJsonDocument<JSON_SIZE_IOT> paramJSON;
StaticJsonDocument<JSON_SIZE_SKETCH> sketchJSON;

and other need to define paramJSON and sketchJSON as extern

on 2nd .h file:

extern JsonDocument paramJSON;
extern JsonDocument paramJSON;

but I get this error:

error: conflicting declaration 'ArduinoJson6172_91::StaticJsonDocument<400u> paramJSON'
 StaticJsonDocument<JSON_SIZE_IOT> paramJSON;

....

: previous declaration as 'ArduinoJson6172_91::JsonDocument paramJSON'
 extern JsonDocument paramJSON;
asked Jan 9, 2021 at 21:49
9
  • 1
    please look at the code snippets that you posted ... the problem appears to be obvious ... the copy'n'paste bug strikes again Commented Jan 10, 2021 at 0:26
  • @jsotola do you mean JsonDocument instead StaticJsonDocument? it does not work either way Commented Jan 10, 2021 at 5:39
  • no ... you made a duplicate declaration Commented Jan 10, 2021 at 5:42
  • 1
    a variable definition should always be in cpp. in h only if you know what you are doing. but in to extern: it must be the same type including the template types. Commented Jan 10, 2021 at 13:16
  • 1
    then put it in ino or add a cpp file Commented Jan 10, 2021 at 13:32

1 Answer 1

1

For an extern the whole definition has to match the "master" definition.

So if you have:

StaticJsonDocument<JSON_SIZE_IOT> paramJSON;

then you extern has to be:

extern StaticJsonDocument<JSON_SIZE_IOT> paramJSON;

Of course you have to make sure your JSON_SIZE_IOT is the same for both - so it's best if that comes from a common source.

This is known as explicit instantiation declaration and you can read more about it in the Class template C++ reference.

answered Jan 11, 2021 at 11:47
2
  • Doesn't StaticJsonDocument a template ? that is why I used JsonDocument . Since JSON_SIZE_IOT defined using #define - how can I use it? ( pre-processor won't pass extern) Commented Jan 11, 2021 at 12:16
  • @Guy.D You can't extern to a different type. If you want to have something else then give yourself a pointer to it and do a cast to that pre-defined object. Commented Jan 11, 2021 at 12:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.