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