std::string greeting = "* " + "hello, " + "!" + " *"

Started by
6 comments, last by Oluseyi 15 years, 5 months ago
Hi, people, I just start learning C++ these days, and i am now facing an extrem serious problem. i am tring to use C++ to write a line like this: * hello, ! * and the followings are my codes, pretty simple, but after i compiled it using VC++ 2008 Express Edition , the compiler told me d:\vcp\accleratedcpp\sayhellov1.1.cpp(12) : error C2110: '+' : cannot add two pointers which means this statement:std::string greeting = "* " + "hello, " + "!" + " *"; , the compiler consider i'm using a * operator as a pointer. :( Plz help me if anyone can figure it out where i coded wrong, tks!!!! // ask for a person's name, and greet the person #include <iostream> #include <string> int main() { //ask for the person's name std::cout<<"Sir, may i have your name, please?"<<std::endl; //read the name std::string name; //define name std::cin>> name; //build the message that we intend to write std::string greeting = "* " + "hello, " + "!" + " *"; //build the second and fourth lines of the ouput const std::string spaces(greeting.size(), ' '); const std::string second = "* " + spaces + " *"; //build the first and fifthe lines of the outpu const std::string first(second.size(), '*'); //write it all std::cout <<std::endl; std::cout <<first<<std::endl; std::cout <<second<<std::endl; std::cout <<greeting<<std::endl; std::cout <<second<<std::endl; std::cout <<first<<std::endl; std::cout<<"Press the enter key to exit"; std::cin.ignore(std::cin.rdbuf()->in_avail()+1); return 0; }
Advertisement
C++ doesn't let you do that unfortunately.

Some alternatives:

One, make the first thing a std::string. Then the remaining items can be literals, and the + will work correctly. This would look like:

std::string greeting = std::string("* ") + "hello, " + "!" + " *";

Second, use a stream. This is probably the more recommended way of doing this kind of thing.

std::stringstream greetingStream;greetingStream << "* " << "hello, " << "!" + " *";std::string = greetingStream.str();

Yes both of those ideas are awkward, but that's C++ for you.

One last option. This one only works if all the items are string literals, but you can just write them next to each other this:

std:::string greeting = "* " "hello, " "!" " *";

and all those strings will be appended. It's not hugely useful because you could just as easily write them as one literal. But it's nice to do this if you need to break up a string across multiple lines.
std::string greeting(std::string("* ") + "hello, " + "!" + " *");

Thats what you want.

the annoymonus std::string("* ")
clues the compiler in on the rest of the line

Amnesty
Thank you two, very much!!
Both of your solution works fine, tks again!
Quote:Original post by cigerchen
which means this statement:std::string greeting = "* " + "hello, " + "!" + " *"; , the compiler consider i'm using a * operator as a pointer. :(

No, it doesn't. What happens is that "* ", "hello, ", "!" and " *" are all pointers (because they're C++ string literals).
hi, after further test,

I tried the following statement:


std::string greeting = "* hello, " + name+"! *";

std::string greeting = ", really?"+name+" *"+ "* ";

std::string greeting = "* really?"+name+" *"+ "* ";

std::string greeting = "* really?" + name+" ** ";

std::string greeting = "* " +"really?"+name+" *"+ "* ";

all worked well, except the last one: std::string greeting = "* " +"really?"+name+" *"+ "* ";
compiler error:
d:\vcp\accleratedcpp\parentheseincpp.cpp(15) : error C2110: '+' : cannot add two pointers

I'm now really confused.. is there any reson for this?

I appreicate anyone can help me for this!
Quote:Original post by cigerchen
I'm now really confused.. is there any reson for this?

I appreicate anyone can help me for this!
As was already said, you can't do it that way in c++.

"abcd" is not a string object. It is a string literal that is constant and cannot be changed. You cannot use the + operator on it.

There are a few automatic conversions that the compiler will do to try to help you, but your examples will not use them.

Your options are to build everything out of strings:

string("* ") + string("hello, ") + string("!") + string(" *");


Or you can turn the first one into a string and the compiler will automatically convert the rest of them:

string("* ") + "hello, " + "!" + " *";


Or you can make them all into proper string objects:

string intro("* ");
string message("hello, ");
string exclamation("!");
string outtro(" *");
string result = intro + message + exclamation + outtro;


What you cannot do is:

"literal one " + "literal two";
Adding two literal constants is not a meaningful operation.
Quote:Original post by frob
"abcd" is not a string object. It is a string literal that is constant and cannot be changed. You cannot use the + operator on it.

operator + does not require a mutable object.

@cigerchen:
"abcd" is not a string literal per se; it's a null-terminated character array, for which operator + is defined in integer terms (because all C-level pointers degrade to an integer value). It is not correct to attempt to concatenate a series of literals via operator +. There is a substantial difference between trying to concatenate two literals and attempting to concatenate a literal and a std::string instance. The latter, for instance, can invoke an overload of operator + that defines the literal as the left-hand side argument.

Basically, the last statement is not even the same kind of operation, so the fact that it behaves differently is unsurprising and normal.

This topic is closed to new replies.

Advertisement