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. 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.
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
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!
* 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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
| |