Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > string + string

Reply
Thread Tools

string + string

 
 
Gary Wessle
Guest
Posts: n/a
 
      12-16-2006
Hi

any idea why I am getting something like


main.cpp:177: error: invalid operands of types ‘const char [11]’ and ‘const char [8]’ to binary ‘operator+’


when trying to compile something like

int main(int argc, char* argv[])
{
string a = ".........." + ".......";
cout << a << endl;
}

how can I solve this, I have few lines and use + to concatenate them
together.

thanks
 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      12-16-2006

Gary Wessle wrote:
> Hi
>
> any idea why I am getting something like
>
>
> main.cpp:177: error: invalid operands of types 'const char [11]' and 'const char [8]' to binary 'operator+'
>
>
> when trying to compile something like
>
> int main(int argc, char* argv[])
> {
> string a = ".........." + ".......";
> cout << a << endl;
> }
>
> how can I solve this, I have few lines and use + to concatenate them
> together.
>
> thanks


You think the above is concatinating literal strings but its actually
attempting to add their pointers. Which is not allowed (obviously) when
constructing the std::string.

try:
std::string a = "..........";
a += ".......";

or better:

std::string sline1(10, '.');
std::string sline2(5, '.');
std::cout << sline1 << " title " << sline2 << std::endl;

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-16-2006
Gary Wessle wrote:
> Hi
>
> any idea why I am getting something like
>
>
> main.cpp:177: error: invalid operands of types ‘const char [11]’ and ‘const char [8]’ to binary ‘operator+’
>
>
> when trying to compile something like
>
> int main(int argc, char* argv[])
> {
> string a = ".........." + ".......";


string a = string("..........") + ".......";
> cout << a << endl;
> }
>
> how can I solve this, I have few lines and use + to concatenate them
> together.


There is no :

string operator+( const char *, const char * )

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-16-2006
Gary Wessle wrote:

> Hi
>
> any idea why I am getting something like
>
>
> main.cpp:177: error: invalid operands of types ‘const char [11]’
> and ‘const char [8]’ to binary ‘operator+’
>
>
> when trying to compile something like
>
> int main(int argc, char* argv[])
> {
> string a = ".........." + ".......";
> cout << a << endl;
> }


There's not a std::string operator + that take two char pointers.

> how can I solve this, I have few lines and use + to concatenate them
> together.


std::string a = ".......";
a += ".....";



Brian
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      12-16-2006
"Gary Wessle" <> wrote in message
news:...
> Hi
>
> any idea why I am getting something like
>
>
> main.cpp:177: error: invalid operands of types 'const char [11]' and
> 'const char [8]' to binary 'operator+'
>
>
> when trying to compile something like
>
> int main(int argc, char* argv[])
> {
> string a = ".........." + ".......";
> cout << a << endl;
> }
>
> how can I solve this, I have few lines and use + to concatenate them
> together.
>
> thanks


As explained, you are actually trying to add two char pointers. A few ways
around:

std::string a = "......";
a += ".....";

or

std::string a = std::string("...........") + "............";


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      12-16-2006
On 16 Dec 2006 16:54:40 +1100, Gary Wessle <> wrote in
comp.lang.c++:

> Hi
>
> any idea why I am getting something like
>
>
> main.cpp:177: error: invalid operands of types ‘const char [11]’ and ‘const char [8]’ to binary ‘operator+’
>
>
> when trying to compile something like
>
> int main(int argc, char* argv[])
> {
> string a = ".........." + ".......";
> cout << a << endl;
> }
>
> how can I solve this, I have few lines and use + to concatenate them
> together.


In addition to Gianni's answer, there is one thing you can do that
only works for string literals in source code, namely leave out the
'+'.

The code:

string a = "abc" "def" "ghi";

....produces exactly the same result as:

string a = "abcdefghi";

And so does this:

string a = "abc"
"def"

"ghi";

String literals that are separated by nothing but white space are
concatenated by the preprocessor when compiling.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments