469,898 Members | 1,613 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

[n00b] Order of strings generates error

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.

Jan 2 '07 #1
6 1298
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 *).

Jan 2 '07 #2

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!

Jan 2 '07 #3
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 ho***@usq.edu.au
http://www.sci.usq.edu.au/staff/house
Jan 2 '07 #4
"Ron House" <ho***@usq.edu.auwrote in message
news:45************@usq.edu.au...
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).

Jan 2 '07 #5

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

Jan 2 '07 #6
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++".
Jan 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Xenophobe | last post: by
12 posts views Thread by Christo | last post: by
9 posts views Thread by copx | last post: by
2 posts views Thread by nuffnough | last post: by
10 posts views Thread by abir | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.