3
\$\begingroup\$

I try to run this example code on my Arduino, but since Arduino IDE version 1.0 it doesn't work no more. The example is mainly copied and stripped down from this example.

void setup() {
  Serial.begin(9600);
}
void loop() {
  // toUpperCase() changes all letters to upper case:
  String stringOne = "test";
  Serial.println(stringOne);
  stringOne = (stringOne.toUpperCase());
  Serial.println(stringOne);

  // do nothing while true:
  while(true);
}

Error message:

StringCaseChanges.cpp: In function ‘void loop()’: StringCaseChanges.cpp:12:39: error: no match for ‘operator=’ in ‘stringOne = stringOne.String::toUpperCase()’ /usr/share/arduino/hardware/arduino/cores/arduino/WString.h:83:11: note: candidates are: String& String::operator=(const String&) /usr/share/arduino/hardware/arduino/cores/arduino/WString.h:84:11: note: String& String::operator=(const char*)

On the help page one can see that:

As of 1.0, toUpperCase() modifies the string in place rather than returning a new one.

However I don't see the reason why the example code doesn't compile.

Can you help me?

Thank you!

//EDIT

OK, this one works: void setup() { Serial.begin(9600); }

void loop() {
  // toUpperCase() changes all letters to upper case:
  String stringOne = "test";
  Serial.println(stringOne);
  stringOne.toUpperCase();
  Serial.println(stringOne);

  // do nothing while true:
  while(true);
}

However, the help page should be changed!

\$\endgroup\$
  • 3
    \$\begingroup\$ You should put your solution in as an answer. \$\endgroup\$ – Majenko Dec 11 '11 at 14:15
  • 5
    \$\begingroup\$ Yes... Please roll back your edit and then place an answer to your question. We will be able to up vote the answer and you can even accept your own answer. \$\endgroup\$ – Kellenjb Dec 11 '11 at 16:03
  • 1
    \$\begingroup\$ The help page looks fine; it's the linked tutorial example that needs to be updated. \$\endgroup\$ – Chris Stratton Dec 11 '11 at 17:21
5
+50
\$\begingroup\$

As of 1.0, toUpperCase() modifies the string in place rather than returning a new one.

However I don't see the reason why the example code doesn't compile.

The thing to note on the help page is that it says "Returns: none". Since the method invocation returns nothing, and you can't assign "nothing" to a string, you get that (somewhat unhelpful) error.

The solution is to simply omit the assignment, as the method invocation itself has done everything you hoped to do, and end up with this line of code:

stringOne.toUpperCase();

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.