Typing problem with ternary operator [solved]

Hi there

I am brandnew to the fascinating tackling with Arduino and I am struggling at one point
(well, the others I could overcome so far)

I have a program with the following code fragments:

#include <HashMap.h>  
CreateHashMap(hashMap, char*, char*, 3); 
...

When I later on try to assign a value to a key using the ternary operator, I get an error during compiling

CODE:

hashMap["§§bgColor§§"] = (bool(digitalRead(ledPin)) ? "ccffcc" : "cccccc");

ERROR:

D:\...\webserver_sample_5.ino: In function 'void handle_ledStatus()':
webserver_sample_5:81: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
   hashMap["§§bgColor§§"] = (bool(digitalRead(ledPin)) ? "ccffcc" : "cccccc");
                          ^
exit status 1
invalid conversion from 'const char*' to 'char*' [-fpermissive]

Whereas when I use a normal if clause, everything works fine:

  if (bool(digitalRead(ledPin))) {
    hashMap["§§bgColor§§"] = "ccffcc";
  } else {
    hashMap["§§bgColor§§"] = "cccccc";
  }

What is happening at that point and what do I have to do to pack it in the ternary?

What means the * within char*? A pointer?

Thanx for helping a bloody beginner!
-Didix

For informed help, please read the post "How to use this forum" and follow the directions.
Code fragments are not helpful.

hashMap["§§bgColor§§"] = (bool(digitalRead(ledPin)) ? "ccffcc" : "cccccc");

It looks like a simple problem of matching parenthesis. Your ternary code is entirely in the (bracketed) condition clause.

Try

hashMap["§§bgColor§§"] = (bool)(digitalRead(ledPin)) ? "ccffcc" : "cccccc";

Or

hashMap["§§bgColor§§"] = ( digitalRead(ledPin) == HIGH ) ? "ccffcc" : "cccccc";

stuart0:

hashMap["§§bgColor§§"] = (bool(digitalRead(ledPin)) ? "ccffcc" : "cccccc");

It looks like a simple problem of matching parenthesis. Your ternary code is entirely in the (bracketed) condition clause.

Try

hashMap["§§bgColor§§"] = (bool)(digitalRead(ledPin)) ? "ccffcc" : "cccccc";

Or

hashMap["§§bgColor§§"] = ( digitalRead(ledPin) == HIGH ) ? "ccffcc" : "cccccc";

I think you need no parentisis:

hashMap["§§bgColor§§"] = digitalRead(ledPin) == HIGH ? "ccffcc" : "cccccc";

From C++ Operator Precedence is said that e = a < d ? a++ : a = d parses as e = ((a < d) ? (a++) : (a = d))!

Thanks ruiseixas. Yes I was wrong about the condition needing parenthesis (like "if" does).

Thank you for supporting!

Unfortunately, none of the three propositions helps.
Same error!

And yes, I will read "How to use this forum" - sorry about that.

-Didix

Try

hashMap["§§bgColor§§"] = (char*)(digitalRead(ledPin) ? "ccffcc" : "cccccc");

or try with

CreateHashMap(hashMap, char*, const char*, 3);

Whandall:
Try

hashMap["§§bgColor§§"] = (char*)(digitalRead(ledPin) ? "ccffcc" : "cccccc");

YES !!

Thanx a lot!

-Didix

Didix:
YES !!

It might compile. But can you actually assign c strings like that? I thought you needed stringcpy().

You are assigning to a HashMap entry, I would think that there is an operator= function (as well as an operator function) in the definition of the object.

stuart0:
It might compile. But can you actually assign c strings like that? I thought you needed stringcpy().

Well, it does not only compile, it really does the job as intended - great :slight_smile:

Didix:
Whereas when I use a normal if clause, everything works fine:

  if (bool(digitalRead(ledPin))) {

hashMap["§§bgColor§§"] = "ccffcc";
 } else {
   hashMap["§§bgColor§§"] = "cccccc";
 }





What is happening at that point and what do I have to do to pack it in the ternary?

Why it worked in the if condition is still a mystery... It should be:

 if (bool(digitalRead(ledPin))) {
    hashMap["§§bgColor§§"] = (char*)"ccffcc";
  } else {
    hashMap["§§bgColor§§"] = (char*)"cccccc";
  }

ruiseixas:
Why it worked in the if condition is still a mystery... It should be:

 if (bool(digitalRead(ledPin))) {

hashMap["§§bgColor§§"] = (char*)"ccffcc";
 } else {
   hashMap["§§bgColor§§"] = (char*)"cccccc";
 }

Yep, that's indeed puzzling - but reality.

Whandall:
You are assigning to a HashMap entry, I would think that there is an operator= function (as well as an operator function) in the definition of the object.

Thanks Whandall. So "=" is like an overloaded operator there.

BTW. Just wondering, why is the (char*) even needed there? Is it to typecast (const char*) to (char*) ?

Yes.

You could/should define the map so it takes const char* instead of char*.