Why does this return fail to work while the other doesn't?

Discussion in 'C++' started by The Cool Giraffe, Feb 13, 2007.

  1. I have these two line of code and they work as they're
    supposed to.

    std::string outPut (ch);

    return outPut;



    Now, i've tried to exchange those two for the one below but

    the computer said no. Why?

    return (std::string outPut (ch));



    Of course i have that char ch[10] and i'm returning
    std:string from the method. I'm using VC++.NET compiler.

    --
    Vänligen Kerstin Viltersten
    (The Cool Giraffe)
     
    The Cool Giraffe, Feb 13, 2007
    #1
    1. Advertising

  2. The Cool Giraffe

    Rolf Magnus Guest

    The Cool Giraffe wrote:

    > I have these two line of code and they work as they're
    > supposed to.
    >
    > std::string outPut (ch);
    >
    > return outPut;
    >
    >
    >
    > Now, i've tried to exchange those two for the one below but
    >
    > the computer said no. Why?


    It just said "no"? That's quite a useless error message.

    > return (std::string outPut (ch));


    A variable definition has no value. You have to use a temporary:

    return std::string(ch);
     
    Rolf Magnus, Feb 13, 2007
    #2
    1. Advertising

  3. The Cool Giraffe wrote:
    > I have these two line of code and they work as they're
    > supposed to.
    >
    > std::string outPut (ch);
    >
    > return outPut;
    >
    >
    >
    > Now, i've tried to exchange those two for the one below but
    >
    > the computer said no. Why?
    >
    > return (std::string outPut (ch));


    Because you're trying to declare an object (named 'outPut') in
    the return statement's expression. That is not allowed. You
    need:

    return std::string(ch);

    which just creates a temporary [unnamed] object of type string
    and returns its value.

    > Of course i have that char ch[10] and i'm returning
    > std:string from the method. I'm using VC++.NET compiler.


    Compiler doesn't matter.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 13, 2007
    #3
  4. On Feb 13, 11:17 am, "The Cool Giraffe" <>
    wrote:

    > I have these two line of code and they work as they're
    > supposed to.
    >
    > std::string outPut (ch);
    >
    > return outPut;
    >
    > Now, i've tried to exchange those two for the one below but
    >
    > the computer said no. Why?
    >
    > return (std::string outPut (ch));


    change that to:

    return std::string(ch);

    and all should be good.

    Best regards,

    Tom
     
    Thomas Tutone, Feb 13, 2007
    #4
  5. The Cool Giraffe

    David Harmon Guest

    On Tue, 13 Feb 2007 17:17:59 +0100 in comp.lang.c++, "The Cool Giraffe"
    <> wrote,
    >Now, i've tried to exchange those two for the one below but
    >
    >the computer said no. Why?
    >
    >return (std::string outPut (ch));


    What is "output" doing there? You can't declare a variable just in the
    middle of an expression.

    return std::string(ch);
     
    David Harmon, Feb 13, 2007
    #5
  6. Thomas Tutone wrote/skrev/kaita/popisal/schreibt :
    > "The Cool Giraffe" <>
    >
    >> I have these two line of code and they work as they're
    >> supposed to.
    >>
    >> std::string outPut (ch);
    >>
    >> return outPut;
    >>
    >> Now, i've tried to exchange those two for the one below but
    >>
    >> the computer said no. Why?
    >>
    >> return (std::string outPut (ch));

    >
    > change that to:
    >
    > return std::string(ch);
    >
    > and all should be good.
    >
    > Best regards,



    Got it. Thanks to all.

    --
    Vänligen Kerstin Viltersten
    (The Cool Giraffe)
     
    The Cool Giraffe, Feb 13, 2007
    #6
  7. The Cool Giraffe

    Alan Johnson Guest

    Rolf Magnus wrote:
    >> the computer said no. Why?

    >
    > It just said "no"? That's quite a useless error message.


    I work with an IDL compiler which has only one error message:
    "You used it wrong."

    --
    Alan Johnson
     
    Alan Johnson, Feb 14, 2007
    #7
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,076
    Smokey Grindel
    Dec 2, 2006
  2. Greenhorn
    Replies:
    15
    Views:
    849
    Keith Thompson
    Mar 6, 2005
  3. Disc Magnet
    Replies:
    3
    Views:
    748
    David Thompson
    Mar 24, 2010
  4. Eric Petzold
    Replies:
    2
    Views:
    108
    bruce
    Jun 11, 2004
  5. Sara
    Replies:
    6
    Views:
    275
    John W. Krahn
    Apr 12, 2004
Loading...

Share This Page