Skip to main content
Added note about ambiguous overload error
Source Link
jose can u c
  • 7k
  • 2
  • 17
  • 27

Your code at the top:

struct Config {
  String ssid = "";
  String pass = "";
  bool hFlag = false;
};

defines a variable "type". It's like making a new int or bool, but yours is called struct Config.

After making the type, you need to create declare an instance of it.

struct Config cfg;

Then, you refer to the one you created:

  // Copy values from the JsonObject to the Config struct named cfg
  cfg.ssid = root["ssid"];
  cfg.pass = root["pass"];
  if (whichFile)
  {
    cfg.hFlag = root["hFlag"];
  }

Note that a particular issue with ArduinoJSON and Strings is that you may get an error like:

error: ambiguous overload for 'operator=' (operand types are 'String' and 'ArduinoJson::JsonObjectSubscript<const char*>')

There is an ArduinoJSON FAQ entry that states

Most of the time you can rely on implicit casts.

But there is one notable exception: when you convert a JsonVariant to a String.

For example:

String ssid = network["ssid"];
ssid = network["ssid"];

The first line will compile but the second will fail with the following error:

error: ambiguous overload for 'operator=' (operand types are 'String' and 'ArduinoJson::JsonObjectSubscript<const char*>')

The solution is to remove the ambiguity with any of the following replacement:

ssid = (const char*)network["ssid"];
ssid = network["ssid"].as<const char*>();
ssid = network["ssid"].as<String>();

Your code at the top:

struct Config {
  String ssid = "";
  String pass = "";
  bool hFlag = false;
};

defines a variable "type". It's like making a new int or bool, but yours is called struct Config.

After making the type, you need to create declare an instance of it.

struct Config cfg;

Then, you refer to the one you created:

  // Copy values from the JsonObject to the Config struct named cfg
  cfg.ssid = root["ssid"];
  cfg.pass = root["pass"];
  if (whichFile)
  {
    cfg.hFlag = root["hFlag"];
  }

Your code at the top:

struct Config {
  String ssid = "";
  String pass = "";
  bool hFlag = false;
};

defines a variable "type". It's like making a new int or bool, but yours is called struct Config.

After making the type, you need to create declare an instance of it.

struct Config cfg;

Then, you refer to the one you created:

  // Copy values from the JsonObject to the Config struct named cfg
  cfg.ssid = root["ssid"];
  cfg.pass = root["pass"];
  if (whichFile)
  {
    cfg.hFlag = root["hFlag"];
  }

Note that a particular issue with ArduinoJSON and Strings is that you may get an error like:

error: ambiguous overload for 'operator=' (operand types are 'String' and 'ArduinoJson::JsonObjectSubscript<const char*>')

There is an ArduinoJSON FAQ entry that states

Most of the time you can rely on implicit casts.

But there is one notable exception: when you convert a JsonVariant to a String.

For example:

String ssid = network["ssid"];
ssid = network["ssid"];

The first line will compile but the second will fail with the following error:

error: ambiguous overload for 'operator=' (operand types are 'String' and 'ArduinoJson::JsonObjectSubscript<const char*>')

The solution is to remove the ambiguity with any of the following replacement:

ssid = (const char*)network["ssid"];
ssid = network["ssid"].as<const char*>();
ssid = network["ssid"].as<String>();
Source Link
jose can u c
  • 7k
  • 2
  • 17
  • 27

Your code at the top:

struct Config {
  String ssid = "";
  String pass = "";
  bool hFlag = false;
};

defines a variable "type". It's like making a new int or bool, but yours is called struct Config.

After making the type, you need to create declare an instance of it.

struct Config cfg;

Then, you refer to the one you created:

  // Copy values from the JsonObject to the Config struct named cfg
  cfg.ssid = root["ssid"];
  cfg.pass = root["pass"];
  if (whichFile)
  {
    cfg.hFlag = root["hFlag"];
  }