Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > [n00b] Order of strings generates error

Reply
Thread Tools

[n00b] Order of strings generates error

 
 
Charles
Guest
Posts: n/a
 
      01-02-2007
I am learning from the Accelerated C++ book. The following example
doesn't work and I don't know why:

#include <iostream>
#include <string>
int main () {
const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;
return 0;
}


But if I invert strings, it works:


#include <iostream>
#include <string>
int main () {
const std::string exclam = "!";
const std::string message = ", world" + exclam + "Hello";
return 0;
}

Unfortunately there's no explanation why. Could you tell me please?
Thanks.

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      01-02-2007
Charles wrote:
> I am learning from the Accelerated C++ book. The following example
> doesn't work and I don't know why:
>
> #include <iostream>
> #include <string>
> int main () {
> const std::string exclam = "!";
> const std::string message = "Hello" + ", world" + exclam;
> return 0;
> }
>
>
> But if I invert strings, it works:
>
>
> #include <iostream>
> #include <string>
> int main () {
> const std::string exclam = "!";
> const std::string message = ", world" + exclam + "Hello";
> return 0;
> }
>
> Unfortunately there's no explanation why. Could you tell me please?
> Thanks.
>


+ associates left to right. So in your first case, you are trying to
add two string literals (char arrays) together, and then add the
exclamation point. There is no operator+ which takes two const char *'s
as parameters.

In the second case, you are "adding" a string literal to a std::string,
and there is an operator+(const char*, const std::string&) defined,
which returns a std::string, so then you add another string literal to
the returned string, using operator+(const std::string&, const char *).

 
Reply With Quote
 
 
 
 
Charles
Guest
Posts: n/a
 
      01-02-2007

red floyd escreveu:
> + associates left to right. So in your first case, you are trying to
> add two string literals (char arrays) together, and then add the
> exclamation point. There is no operator+ which takes two const char *'s
> as parameters.
>
> In the second case, you are "adding" a string literal to a std::string,
> and there is an operator+(const char*, const std::string&) defined,
> which returns a std::string, so then you add another string literal to
> the returned string, using operator+(const std::string&, const char *).


Ah ok, thank you!

 
Reply With Quote
 
Ron House
Guest
Posts: n/a
 
      01-02-2007
Charles wrote:
> I am learning from the Accelerated C++ book. The following example
> doesn't work and I don't know why:


What do you mean "doesn't work"??? Compiler error? What is it? Runtime
error, wrong output, what? If you want help, please write a coherent
account of your problem. Putting "n00b" in the title is not an excuse
for laziness, only for ignorance.

> #include <iostream>
> #include <string>
> int main () {
> const std::string exclam = "!";
> const std::string message = "Hello" + ", world" + exclam;
> return 0;
> }


But luckily, the problem is obvious. The + operator is left-associative.
Putting + between two C-style literals is attempting to call the
(nonexistent) operator that adds two char pointers. The above could be
fixed by writing:

"Hello" + (", world" + exclam);

--
Ron House
http://www.sci.usq.edu.au/staff/house
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-02-2007
"Ron House" <> wrote in message
news:...
> Charles wrote:
>> I am learning from the Accelerated C++ book. The following example
>> doesn't work and I don't know why:

>
> What do you mean "doesn't work"??? Compiler error? What is it? Runtime
> error, wrong output, what? If you want help, please write a coherent
> account of your problem. Putting "n00b" in the title is not an excuse for
> laziness, only for ignorance.
>
>> #include <iostream>
>> #include <string>
>> int main () {
>> const std::string exclam = "!";
>> const std::string message = "Hello" + ", world" + exclam;
>> return 0;
>> }

>
> But luckily, the problem is obvious. The + operator is left-associative.
> Putting + between two C-style literals is attempting to call the
> (nonexistent) operator that adds two char pointers. The above could be
> fixed by writing:
>
> "Hello" + (", world" + exclam);


You know, this question comes up quite a bit, and it even bit me a few times
til I fugred it out. I believe that in C and C++ adding two pointers is
undefined behavior. You can add an int to a pointer, but not a pointer to a
pointer. So, I've been thinking, would it make sense to make an
operator+(const char*, const char*) that would return a std::string? What
would be the drawbacks to this (other than the possible difficulty of
implementing it).



 
Reply With Quote
 
Ivan Novick
Guest
Posts: n/a
 
      01-02-2007

Jim Langston wrote:
> You know, this question comes up quite a bit, and it even bit me a few times
> til I fugred it out. I believe that in C and C++ adding two pointers is
> undefined behavior. You can add an int to a pointer, but not a pointer to a
> pointer. So, I've been thinking, would it make sense to make an
> operator+(const char*, const char*) that would return a std::string? What
> would be the drawbacks to this (other than the possible difficulty of
> implementing it).


Can you give an example where this functionality would be practical to
use? The example given above is not a good use case because you can
just do the following and the string literals will be concatenated
without having to construct an object:

std::string one = "Santa";
std::string two = "Hello World " "from " + one;

----
Ivan
http://www.0x4849.net

 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      01-02-2007
On Tue, 02 Jan 2007 13:10:10 +1000, Ron House wrote:
>Charles wrote:
>> I am learning from the Accelerated C++ book. The following example
>> doesn't work and I don't know why:

>
>> #include <iostream>
>> #include <string>
>> int main () {
>> const std::string exclam = "!";
>> const std::string message = "Hello" + ", world" + exclam;
>> return 0;
>> }

>
>But luckily, the problem is obvious.


The problem is obviously not obvious for someone who learns C++ from
"Accelerated C++".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Long call to webservice generates "underlying connection was closed: An unexpected error occurred on a receive."Error Techsatish ASP .Net Web Services 1 09-22-2006 03:18 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
External Assembly accesing Session generates an error =?Utf-8?B?cmFzMjY=?= ASP .Net 2 05-16-2006 03:22 PM
Internet explorer will not open, generates error box Tom Tricep Computer Support 1 10-19-2005 11:19 PM
Impersonation generates "Invalid Cast" error Peter Afonin ASP .Net 0 08-30-2003 10:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57