473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question on adding strings

Hi,

Consider the following program from "Accelerated C++", Koenig & Moo
[2004]:

Case [1]:
=====================================
#include <iostream>
#include <string>

int main()
{

// Ex 1-2
const std::string exclam = "!";
const std::string message2 = "Hello" + ", world" + exclam ;
std::cout << message2;
std::cout << std::endl;
return 0;
}
=====================================

The above fails to compile with the error:
1-3.cpp: In function 'int main()':
1-3.cpp:18: error: invalid operands of types 'const char [6]' and
'const char [8]' to binary 'operator+'

(Line numbers are a bit off as I snipped out irrelevant code)

If we move around the string addition a bit to a less sensible
arrangement:
Case 2:
=======================================
#include <iostream>
#include <string>

int main()
{

// Ex 1-1
const std::string hello = "Hello";
std::cout << hello;
std::cout << std::endl;

const std:: string message = hello + ", world" + "!";
std::cout << message;
std::cout << std::endl;

// Ex 1-2
const std::string exclam = "!";
const std::string message2 = "Hello" + exclam + ", world" ;
std::cout << message2;
std::cout << std::endl;

return 0;
}
====================================

The above compiles and runs.

Why does changing the arrangement of message2 make such a big
difference?

Thanks for any input.
Mar 31 '08 #1
5 1732
On Mar 31, 9:50 am, "C++ Newbie" <newbie....@googlemail.comwrote:
Hi,

Consider the following program from "Accelerated C++", Koenig & Moo
[2004]:

Case [1]:
=====================================
#include <iostream>
#include <string>

int main()
{

// Ex 1-2
const std::string exclam = "!";
const std::string message2 = "Hello" + ", world" + exclam ;
std::cout << message2;
std::cout << std::endl;
return 0;}

=====================================

The above fails to compile with the error:
1-3.cpp: In function 'int main()':
1-3.cpp:18: error: invalid operands of types 'const char [6]' and
'const char [8]' to binary 'operator+'

(Line numbers are a bit off as I snipped out irrelevant code)

If we move around the string addition a bit to a less sensible
arrangement:
Case 2:
=======================================
#include <iostream>
#include <string>

int main()
{

// Ex 1-1
const std::string hello = "Hello";
std::cout << hello;
std::cout << std::endl;

const std:: string message = hello + ", world" + "!";
std::cout << message;
std::cout << std::endl;

// Ex 1-2
const std::string exclam = "!";
const std::string message2 = "Hello" + exclam + ", world" ;
std::cout << message2;
std::cout << std::endl;

return 0;}

====================================

The above compiles and runs.

Why does changing the arrangement of message2 make such a big
difference?

Thanks for any input.

Because, char* does not have operator +(const char* &) defined, while
std::string does.
Mar 31 '08 #2
C++ Newbie wrote:
Hi,

Consider the following program from "Accelerated C++", Koenig & Moo
[2004]:

Case [1]:
=====================================
#include <iostream>
#include <string>

int main()
{

// Ex 1-2
const std::string exclam = "!";
const std::string message2 = "Hello" + ", world" + exclam ;
std::cout << message2;
std::cout << std::endl;
return 0;
}
=====================================

The above fails to compile with the error:
1-3.cpp: In function 'int main()':
1-3.cpp:18: error: invalid operands of types 'const char [6]' and
'const char [8]' to binary 'operator+'
Like the compiler says, there is no operator+ for string literals. Why
would you need one?

You can write either:
const std::string message2 = "Hello, world" + exclam ;
or (with no plus)
const std::string message2 = "Hello" ", world" + exclam ;

and it just works.
Bo Persson
Mar 31 '08 #3
On 31 Mar, 17:02, "Bo Persson" <b...@gmb.dkwrote:
C++ Newbie wrote:
Hi,
Consider the following program from "Accelerated C++", Koenig & Moo
[2004]:
Case [1]:
=====================================
#include <iostream>
#include <string>
int main()
{
// Ex 1-2
const std::string exclam = "!";
const std::string message2 = "Hello" + ", world" + exclam ;
std::cout << message2;
std::cout << std::endl;
return 0;
}
=====================================
The above fails to compile with the error:
1-3.cpp: In function 'int main()':
1-3.cpp:18: error: invalid operands of types 'const char [6]' and
'const char [8]' to binary 'operator+'

Like the compiler says, there is no operator+ for string literals. Why
would you need one?

You can write either:
const std::string message2 = "Hello, world" + exclam ;
or (with no plus)
const std::string message2 = "Hello" ", world" + exclam ;

and it just works.

Bo Persson
OK thanks guys!
Apr 1 '08 #4
* C++ Newbie:
>
Consider the following program from "Accelerated C++", Koenig & Moo
[2004]:

Case [1]:
=====================================
#include <iostream>
#include <string>

int main()
{

// Ex 1-2
const std::string exclam = "!";
const std::string message2 = "Hello" + ", world" + exclam ;
std::cout << message2;
std::cout << std::endl;
return 0;
}
I'll take your word for it that this code appears in the book. But is it there
an example of code that's meant to compile, or is it presented as an example of
something that should not / does not compile?

Cheers,

- Alf
Apr 1 '08 #5
On 1 Apr, 10:19, "Alf P. Steinbach" <al...@start.nowrote:
* C++ Newbie:


Consider the following program from "Accelerated C++", Koenig & Moo
[2004]:
Case [1]:
=====================================
#include <iostream>
#include <string>
int main()
{
// Ex 1-2
const std::string exclam = "!";
const std::string message2 = "Hello" + ", world" + exclam ;
std::cout << message2;
std::cout << std::endl;
return 0;
}

I'll take your word for it that this code appears in the book. But is it there
an example of code that's meant to compile, or is it presented as an example of
something that should not / does not compile?

Cheers,

- Alf
Hi Alf,

It is an exercise question in Chapter 1 (specifically Ex 1-2). The
reader is asked if the following definitions were valid.
Apr 1 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sean Berry | last post by:
I have two lists... one like the following, list1 ... and the other like this, list2 ... , ] Both lists are much more extensive, the first being a list of about 10 strings, and the...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
11
by: sneill | last post by:
I have read a number of posts on the use of eval() in Javascript, and I agree that its use is questionable. But it does beg the following question: "How arbitrary does a string need to be before...
3
by: Colin Savage | last post by:
I am trying to work out the best way to use the NameTable class in my C# application. I am assuming that getting/adding a string to the nametable has the same overheads as comparing a string...
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
3
by: CharlesA | last post by:
knowing only how to do C# web apps and nothing about the other standards out there.... anyone care to enlighten me about how CGI works? are (Perl and CGI the same thing) is it just a language that...
3
by: mr | last post by:
How can i 'force' c++ to interprete "blabla" strings as unicode string instead of ascii string (i just don't want to add 'L' before the thousands strings that are on my projects...), as all my...
73
by: JoeC | last post by:
I am writing a game and I am having a challenge with my combat function. All I want to do is find out how to group pieces that are in the same space. There are two sides and all the units that...
2
by: Andy | last post by:
I want to dynamically update a list of elements shown as a checkbox list. A file is used to store the elements, and elements can be added and deleted from the list. The trouble is that the window...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.