string concatenation

Discussion in 'C++' started by Gernot Frisch, Dec 3, 2004.

  1. Hi,

    std::string a("a"), b("b");

    This doesn't seem to work, due to a missing '+' operator in
    std::string.
    printf("%s", (a+b).c_str());

    How do you handle such situations?
    std::string tmp(a); tmp+=b; ??

    Thank you,
    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
     
    Gernot Frisch, Dec 3, 2004
    #1
    1. Advertising

  2. Gernot Frisch wrote:
    >
    > Hi,
    >
    > std::string a("a"), b("b");
    >
    > This doesn't seem to work, due to a missing '+' operator in
    > std::string.
    > printf("%s", (a+b).c_str());
    >


    What are you talking about? The above is done numerous
    times all over the world each day.

    Did you again forget to #include <string> ?

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Dec 3, 2004
    #2
    1. Advertising

  3. > This doesn't seem to work, due to a missing '+' operator in
    > std::string.
    > printf("%s", (a+b).c_str());


    The operator+ is *not* missing in std::string.
    In this case, (a+b) returns a new std::string which is a concatenation of
    'a' and 'b' and from which you take the address to its internal string data
    (which is a "C-style string"). That's how it works, the error must be
    lurking elsewhere.
     
    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Dec 3, 2004
    #3
  4. "Karl Heinz Buchegger" <> schrieb im Newsbeitrag
    news:...
    > Gernot Frisch wrote:
    >>
    >> Hi,
    >>
    >> std::string a("a"), b("b");
    >>
    >> This doesn't seem to work, due to a missing '+' operator in
    >> std::string.
    >> printf("%s", (a+b).c_str());
    >>

    >
    > What are you talking about? The above is done numerous
    > times all over the world each day.
    >
    > Did you again forget to #include <string> ?


    Oh wow. That was it. The error from before gave me this as well. And
    intellisense is too stupid to show the '+' operator, so I was fooled
    twice.
    Thank you,
    Gernot
     
    Gernot Frisch, Dec 3, 2004
    #4
  5. Gernot Frisch wrote:
    >
    >>times all over the world each day.
    >>
    >>Did you again forget to #include <string> ?

    >
    >
    > Oh wow. That was it. The error from before gave me this as well. And
    > intellisense is too stupid to show the '+' operator, so I was fooled
    > twice.


    Also be careful to have it included in your .h files, should you
    have any. You often might have functions declared in a .h file
    that return or accept a string. Forgetting to #include<string>
    there leads to a few screenfuls of meaningless error messages
    as well, and a different variety.

    HTH,
    - J.
     
    Jacek Dziedzic, Dec 3, 2004
    #5
    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. walala
    Replies:
    3
    Views:
    4,809
    walala
    Sep 18, 2003
  2. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,258
    Joe Smith
    Apr 5, 2004
  3. Daniel Bergquist

    String Concatenation problems

    Daniel Bergquist, Jul 13, 2004, in forum: Perl
    Replies:
    2
    Views:
    499
    Joe Smith
    Jul 16, 2004
  4. Sparky Arbuckle

    String Concatenation & Removing Space

    Sparky Arbuckle, Sep 1, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    617
    Sparky Arbuckle
    Sep 1, 2005
  5. Andrew Berg
    Replies:
    13
    Views:
    1,299
    Andrew Berg
    Jul 11, 2011
Loading...

Share This Page