Question on adding strings

C

C++ Newbie

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.
 
C

Christopher

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.
 
B

Bo Persson

C++ Newbie said:
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
 
A

Abhishek Padmanabh

C++ Newbie said:
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?

The difference here is caused by the rules for operator associativity. The
operator in question here is operator+ that, I think, has left-to-right
associativity. So, for the first case, when the evaluation starts, it picks
up two the two string literals that don't have the operator+ defined for
them. For the other two cases, one argument is std::string that has two
operator+'s defined that returns back another std::string which then gets
evaluated with the right-most argument and it works its way out.

std::string operator+(const char* str1, const std::string& str2);

and

std::string operator+(const std::string& str1, const char* str2);
 
A

Abhishek Padmanabh

"C++ Newbie" <[email protected]>
       std::string operator+(const char* str1, const std::string& str2);
and
      std::string operator+(const std::string& str1, const char* str2);- Hide quoted text -

Sorry, that's probably not the exact prototype but gives an idea of
what I meant. Temporary string objects are created out of the string
literals that get passed into the operator+ non-members for
std::string (basic_string actually).
 
C

C++ Newbie

C++ Newbie said:
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!
 
A

Alf P. Steinbach

* 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
 
C

C++ Newbie

* 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

CIN Input #2 gets skipped, I don't understand why. 1
Lexical Analysis on C++ 1
Crossword 2
Character operations in C++ 2
TF-IDF 1
Crossword 14
[n00b] Order of strings generates error 6
Remove Space, Stuck on lab 1

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top