string + string

G

Gary Wessle

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
 
S

Salt_Peter

Gary said:
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;
 
G

Gianni Mariani

Gary said:
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 * )
 
D

Default User

Gary said:
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
 
J

Jim Langston

Gary Wessle said:
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("...........") + "............";
 
J

Jack Klein

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.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top