Home » Community » Newbie corner » error C2110: '+' : cannot add two pointers
error C2110: '+' : cannot add two pointers [message #30445] |
Sat, 01 January 2011 11:32 |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
I ran into this and it took a couple hours to figure it out.
Here is a very simple example:
Ln0 = "ab" + "cd";
Where it actually came up was:
Ln0 = "clear64\n" + "gc," + Pt1 + "," + Pt2 + "\n";
This is C++, pointer addresses cannot be added.
This works:
Ln0 = "clear64\ngc," + Pt1 + "," + Pt2 + "\n";
The "," and "\n" are added OK when there is a String in between but
"," + "\n" will throw the error.
Quote: | The reason is because the system does not have a way to add a const char* and a string ("stuff" + string), but string has a way to add a string and a const char* (string + "stuff").
|
http://www.cplusplus.com/forum/beginner/6334/
Ln0 = Pt1 + ",";
This is OK, the String Pt1 has "," added.
If Pt1 is "pt" then Ln0 = "pt,"
Pt1 + "," + "a" gives an error.
Seems like this should give Ln0 = "pt,a"
A String and a const* char* "," is added OK.
Why can't another const* char* "a" be added to the string?
Is there a simple explanation.
|
|
|
Re: error C2110: '+' : cannot add two pointers [message #30447 is a reply to message #30445] |
Sat, 01 January 2011 19:09 |
|
Hi Neil!
The problem is that there is no operator that would allow to add two const char*. Usually as in your case, those are literal strings so it is not much of a problem since you can always write "ab" instead of "a" + "b". For Upp::String (and for std::string too) there are various operators that allow for operations with both other Strings and const char*.
Also note that there are some other tricks that can help in some situations:String s,str;
const char* c="something";
s="a"+"b"; //fails
s="a" "b"; //works
s="a"+"b"+str; //fails
s="a"+("b"+str); //works
s=c+c; //fails
s=String(c)+c; //works
The first one is not so known property of C, which was probably designed to allow splitting long strings on multiple lines The second and third are simple, you sure understand what happens there
|
|
|
Re: error C2110: '+' : cannot add two pointers [message #30455 is a reply to message #30445] |
Sun, 02 January 2011 09:31 |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
I don't recall using any of the three examples.
Most of my experience with C++ was more than 15 years ago, just getting back into it in the last year.
In Java I don't think this is a problem.
In Python switching from 2.xx to 3.xx ran into something:
OKCom = OKCom + [ser.portstr + ' ' + str(Baud[b]) + ' OK']
In 2.xx
+ Baud[b] + worked
but 3.xx requires
+ str(Baud[b]) +
It looks like I have additional reading/research to do.
Thanks
Neil
|
|
|
Goto Forum:
Current Time: Sat Apr 27 08:39:29 CEST 2024
Total time taken to generate the page: 0.05307 seconds
|