jung6717 / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

String + operator does not work with ints #304

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
This code:
void loop() {
  String stringOne = "this";
  for (int charNum = 0; charNum < stringOne.length(); charNum++) {
    char thisChar = stringOne.charAt(charNum);
    Serial.println("Character " + charNum + " of " + stringOne + " is " + thisChar);
  }
}

can be fixed by casting charNum to a String.  

What is the expected output? What do you see instead?
Expected:
String character functions:
Character 0 of this is t
Character 1 of this is h
Character 2 of this is i
Character 3 of this is s

Got:
StringCharacterFunctions.cpp: In function 'void loop()':
StringCharacterFunctions:25: error: invalid operands of types 'const char*' and 
'const char [5]' to binary 'operator+'

Original issue reported on code.google.com by tom.i...@gmail.com on 27 Jul 2010 at 8:26

GoogleCodeExporter commented 9 years ago
Yeah, this is an unfortunate aspect of C++: we can't overload the behavior of 
the + (or other) operators on the built-in types.  So if you want to add a 
char* (like "Character: ") and an int, it will do pointer arithmetic instead of 
concatenating strings.  So, there's basically no way to get 
Serial.println("value: " + x); to do what we want (if x is a built-in type like 
an int).  Because of this, we could totally avoid the use of the + and +=, etc. 
operators with Strings, so people don't expect them to also work on constant 
strings (i.e. char* like "hello").  What do you think?

Original comment by dmel...@gmail.com on 27 Jul 2010 at 9:25

GoogleCodeExporter commented 9 years ago
It works with some strings, for example:

 stringOne = "<HTML><HEAD><BODY>";
  int firstClosingBracket = stringOne.indexOf('>');
  Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket);

But not with others.  For example, I need a cast in what's posted above. I can 
see the value of + and +=, I'm using it all over the place in the examples.  
But order definitely matters. You have to have the int at the end.

If we avoid them, what would we use instead?

Original comment by tom.i...@gmail.com on 27 Jul 2010 at 9:44

GoogleCodeExporter commented 9 years ago
In theory, it should work if either of the two operands of the + or += 
operation are instances of the String class (as opposed to "constant strings"). 
 What happens if you try it with just two variables, String + int or int + 
String, etc?

Original comment by dmel...@gmail.com on 28 Jul 2010 at 12:27

GoogleCodeExporter commented 9 years ago
Works fine in those conditions.  You can see in the examples several cases 
where it's not a problem.  I think that one case is the only one where it is an 
issue.  What's operator precedence on X + X + X?  It's (X + X) + X, right?

Original comment by tom.i...@gmail.com on 28 Jul 2010 at 1:05

GoogleCodeExporter commented 9 years ago
Yeah, I think so.  Which would make the first operation in the original post 
start with ("Character " + charNum), which is a char * and an int.  That gives 
another char * (incremented in memory by the charNum), which is then supposed 
to be added to the another one ("of ").  I think that's what the error message 
refers to.

Actually, now that I think about it some more, this might be a good excuse for 
introducing a wrapper around constant strings (literals like "hello"), which 
could store them in program memory and also wrap them in a class that's 
compatible with String in some way (i.e. at least the operators like + would 
work on it).  So you would write something like S("hello") instead of "hello".  
What do you think?  (This might have to wait a bit to get implemented though.)

Original comment by dmel...@gmail.com on 28 Jul 2010 at 1:46

GoogleCodeExporter commented 9 years ago
I think that's awkward.  People are used to quoted literals, we should find a 
way to accommodate them, or provide a bug workaround for special cases only. 
For example, casting them to a String, which works well. It could be as simple 
as putting that in the docs for Strings and quoted literals:  "When you have 
problems with quoted literals, try casting them to a String"

Original comment by tom.i...@gmail.com on 28 Jul 2010 at 1:52

GoogleCodeExporter commented 9 years ago
It sounds like this isn't something we can fix in the String class, right?  So 
I'm closing the bug, but feel free to re-open (or start a new one) if I'm 
missing something.

Original comment by dmel...@gmail.com on 2 Aug 2010 at 7:28

GoogleCodeExporter commented 9 years ago
I think so.  Take a look at the stuff I wrote yesterday, I tried to document 
around the issue.  Dunno if I succeeded.

Original comment by tom.i...@gmail.com on 2 Aug 2010 at 7:35