Didix
January 29, 2017, 4:21pm
#1
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.
stuart0
January 29, 2017, 4:39pm
#3
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))
!
stuart0
January 29, 2017, 5:04pm
#5
Thanks ruiseixas. Yes I was wrong about the condition needing parenthesis (like "if" does).
Didix
January 29, 2017, 5:08pm
#6
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");
J-M-L
January 29, 2017, 5:13pm
#8
or try with
CreateHashMap(hashMap, char*, const char*, 3);
stuart0
January 29, 2017, 5:33pm
#10
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.
Didix
January 29, 2017, 5:42pm
#12
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
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";
}
Didix
January 29, 2017, 5:52pm
#14
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.
stuart0
January 29, 2017, 5:56pm
#15
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.
stuart0
January 29, 2017, 6:02pm
#16
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*.