Question on adding strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C++ Newbie

    Question on adding strings

    Hi,

    Consider the following program from "Accelerate d C++", Koenig & Moo
    [2004]:

    Case [1]:
    =============== =============== =======
    #include <iostream>
    #include <string>

    int main()
    {

    // Ex 1-2
    const std::string exclam = "!";
    const std::string message2 = "Hello" + ", world" + exclam ;
    std::cout << message2;
    std::cout << std::endl;
    return 0;
    }
    =============== =============== =======

    The above fails to compile with the error:
    1-3.cpp: In function 'int main()':
    1-3.cpp:18: error: invalid operands of types 'const char [6]' and
    'const char [8]' to binary 'operator+'

    (Line numbers are a bit off as I snipped out irrelevant code)

    If we move around the string addition a bit to a less sensible
    arrangement:
    Case 2:
    =============== =============== =========
    #include <iostream>
    #include <string>

    int main()
    {

    // Ex 1-1
    const std::string hello = "Hello";
    std::cout << hello;
    std::cout << std::endl;

    const std:: string message = hello + ", world" + "!";
    std::cout << message;
    std::cout << std::endl;

    // Ex 1-2
    const std::string exclam = "!";
    const std::string message2 = "Hello" + exclam + ", world" ;
    std::cout << message2;
    std::cout << std::endl;

    return 0;
    }
    =============== =============== ======

    The above compiles and runs.

    Why does changing the arrangement of message2 make such a big
    difference?

    Thanks for any input.
  • Christopher

    #2
    Re: Question on adding strings

    On Mar 31, 9:50 am, "C++ Newbie" <newbie....@goo glemail.comwrot e:
    Hi,
    >
    Consider the following program from "Accelerate d C++", Koenig & Moo
    [2004]:
    >
    Case [1]:
    =============== =============== =======
    #include <iostream>
    #include <string>
    >
    int main()
    {
    >
    // Ex 1-2
    const std::string exclam = "!";
    const std::string message2 = "Hello" + ", world" + exclam ;
    std::cout << message2;
    std::cout << std::endl;
    return 0;}
    >
    =============== =============== =======
    >
    The above fails to compile with the error:
    1-3.cpp: In function 'int main()':
    1-3.cpp:18: error: invalid operands of types 'const char [6]' and
    'const char [8]' to binary 'operator+'
    >
    (Line numbers are a bit off as I snipped out irrelevant code)
    >
    If we move around the string addition a bit to a less sensible
    arrangement:
    Case 2:
    =============== =============== =========
    #include <iostream>
    #include <string>
    >
    int main()
    {
    >
    // Ex 1-1
    const std::string hello = "Hello";
    std::cout << hello;
    std::cout << std::endl;
    >
    const std:: string message = hello + ", world" + "!";
    std::cout << message;
    std::cout << std::endl;
    >
    // Ex 1-2
    const std::string exclam = "!";
    const std::string message2 = "Hello" + exclam + ", world" ;
    std::cout << message2;
    std::cout << std::endl;
    >
    return 0;}
    >
    =============== =============== ======
    >
    The above compiles and runs.
    >
    Why does changing the arrangement of message2 make such a big
    difference?
    >
    Thanks for any input.

    Because, char* does not have operator +(const char* &) defined, while
    std::string does.

    Comment

    • Bo Persson

      #3
      Re: Question on adding strings

      C++ Newbie wrote:
      Hi,
      >
      Consider the following program from "Accelerate d C++", Koenig & Moo
      [2004]:
      >
      Case [1]:
      =============== =============== =======
      #include <iostream>
      #include <string>
      >
      int main()
      {
      >
      // Ex 1-2
      const std::string exclam = "!";
      const std::string message2 = "Hello" + ", world" + exclam ;
      std::cout << message2;
      std::cout << std::endl;
      return 0;
      }
      =============== =============== =======
      >
      The above fails to compile with the error:
      1-3.cpp: In function 'int main()':
      1-3.cpp:18: error: invalid operands of types 'const char [6]' and
      'const char [8]' to binary 'operator+'
      >
      Like the compiler says, there is no operator+ for string literals. Why
      would you need one?

      You can write either:
      const std::string message2 = "Hello, world" + exclam ;
      or (with no plus)
      const std::string message2 = "Hello" ", world" + exclam ;

      and it just works.


      Bo Persson


      Comment

      • C++ Newbie

        #4
        Re: Question on adding strings

        On 31 Mar, 17:02, "Bo Persson" <b...@gmb.dkwro te:
        C++ Newbie wrote:
        Hi,
        >
        Consider the following program from "Accelerate d C++", Koenig & Moo
        [2004]:
        >
        Case [1]:
        =============== =============== =======
        #include <iostream>
        #include <string>
        >
        int main()
        {
        >
        // Ex 1-2
        const std::string exclam = "!";
        const std::string message2 = "Hello" + ", world" + exclam ;
        std::cout << message2;
        std::cout << std::endl;
        return 0;
        }
        =============== =============== =======
        >
        The above fails to compile with the error:
        1-3.cpp: In function 'int main()':
        1-3.cpp:18: error: invalid operands of types 'const char [6]' and
        'const char [8]' to binary 'operator+'
        >
        Like the compiler says, there is no operator+ for string literals. Why
        would you need one?
        >
        You can write either:
        const std::string message2 = "Hello, world" + exclam ;
        or (with no plus)
        const std::string message2 = "Hello" ", world" + exclam ;
        >
        and it just works.
        >
        Bo Persson
        OK thanks guys!

        Comment

        • Alf P. Steinbach

          #5
          Re: Question on adding strings

          * C++ Newbie:
          >
          Consider the following program from "Accelerate d C++", Koenig & Moo
          [2004]:
          >
          Case [1]:
          =============== =============== =======
          #include <iostream>
          #include <string>
          >
          int main()
          {
          >
          // Ex 1-2
          const std::string exclam = "!";
          const std::string message2 = "Hello" + ", world" + exclam ;
          std::cout << message2;
          std::cout << std::endl;
          return 0;
          }
          I'll take your word for it that this code appears in the book. But is it there
          an example of code that's meant to compile, or is it presented as an example of
          something that should not / does not compile?



          Cheers,

          - Alf

          Comment

          • C++ Newbie

            #6
            Re: Question on adding strings

            On 1 Apr, 10:19, "Alf P. Steinbach" <al...@start.no wrote:
            * C++ Newbie:
            >
            >
            >
            >
            >
            Consider the following program from "Accelerate d C++", Koenig & Moo
            [2004]:
            >
            Case [1]:
            =============== =============== =======
            #include <iostream>
            #include <string>
            >
            int main()
            {
            >
            // Ex 1-2
            const std::string exclam = "!";
            const std::string message2 = "Hello" + ", world" + exclam ;
            std::cout << message2;
            std::cout << std::endl;
            return 0;
            }
            >
            I'll take your word for it that this code appears in the book. But is it there
            an example of code that's meant to compile, or is it presented as an example of
            something that should not / does not compile?
            >
            Cheers,
            >
            - Alf
            Hi Alf,

            It is an exercise question in Chapter 1 (specifically Ex 1-2). The
            reader is asked if the following definitions were valid.

            Comment

            Working...