string + string

Discussion in 'C++' started by Gary Wessle, Dec 16, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    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
     
    Gary Wessle, Dec 16, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Salt_Peter Guest

    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;
     
    Salt_Peter, Dec 16, 2006
    #2
    1. Advertising

  3. 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 * )
     
    Gianni Mariani, Dec 16, 2006
    #3
  4. Gary Wessle

    Default User Guest

    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
     
    Default User, Dec 16, 2006
    #4
  5. Gary Wessle

    Jim Langston Guest

    "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("...........") + "............";
     
    Jim Langston, Dec 16, 2006
    #5
  6. Gary Wessle

    Jack Klein Guest

    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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 16, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    777
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,772
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    548
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    8,061
    John C. Bollinger
    Nov 19, 2004
  5. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,818
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
Loading...

Share This Page