 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gary Wessle Guest
|
Posted: Sat Dec 16, 2006 10:10 am Post subject: string + string |
|
|
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 |
|
| Back to top |
|
 |
Default User Guest
|
Posted: Sat Dec 16, 2006 10:10 am Post subject: Re: string + string |
|
|
Gary Wessle wrote:
| Quote: | 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.
| Quote: | how can I solve this, I have few lines and use + to concatenate them
together.
|
std::string a = ".......";
a += ".....";
Brian |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Sat Dec 16, 2006 10:10 am Post subject: Re: string + string |
|
|
Gary Wessle wrote:
| Quote: | 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; |
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Sat Dec 16, 2006 10:10 am Post subject: Re: string + string |
|
|
Gary Wessle wrote:
| Quote: | 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("..........") + ".......";
| Quote: | 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 * ) |
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Sat Dec 16, 2006 10:11 am Post subject: Re: string + string |
|
|
On 16 Dec 2006 16:54:40 +1100, Gary Wessle <phddas (AT) yahoo (DOT) com> wrote in
comp.lang.c++:
| Quote: | 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/~ajo/docs/FAQ-acllc.html |
|
| Back to top |
|
 |
Jim Langston Guest
|
Posted: Sat Dec 16, 2006 10:11 am Post subject: Re: string + string |
|
|
"Gary Wessle" <phddas (AT) yahoo (DOT) com> wrote in message
news:m31wn0peqn.fsf (AT) localhost (DOT) localdomain...
| Quote: | 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("...........") + "............"; |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|