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.
    The Cool Giraffe, Feb 13, 2007
    1. Advertisements

  2. The Cool Giraffe

    Rolf Magnus Guest

    It just said "no"? That's quite a useless error message.
    A variable definition has no value. You have to use a temporary:

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

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

    return std::string(ch);

    which just creates a temporary [unnamed] object of type string
    and returns its value.
    Compiler doesn't matter.

    Victor Bazarov, Feb 13, 2007
  4. change that to:

    return std::string(ch);

    and all should be good.

    Best regards,

    Thomas Tutone, Feb 13, 2007
  5. The Cool Giraffe

    David Harmon Guest

    On Tue, 13 Feb 2007 17:17:59 +0100 in comp.lang.c++, "The Cool Giraffe"
    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
  6. Thomas Tutone wrote/skrev/kaita/popisal/schreibt :

    Got it. Thanks to all.
    The Cool Giraffe, Feb 13, 2007
  7. The Cool Giraffe

    Alan Johnson Guest

    I work with an IDL compiler which has only one error message:
    "You used it wrong."
    Alan Johnson, Feb 14, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.