Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why does this return fail to work while the other doesn't?

Reply
Thread Tools

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

 
 
The Cool Giraffe
Guest
Posts: n/a
 
      02-13-2007
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)


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-13-2007
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);

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-13-2007
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


 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      02-13-2007
On Feb 13, 11:17 am, "The Cool Giraffe" <(E-Mail Removed)>
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


 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      02-13-2007
On Tue, 13 Feb 2007 17:17:59 +0100 in comp.lang.c++, "The Cool Giraffe"
<(E-Mail Removed)> 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);

 
Reply With Quote
 
The Cool Giraffe
Guest
Posts: n/a
 
      02-13-2007
Thomas Tutone wrote/skrev/kaita/popisal/schreibt :
> "The Cool Giraffe" <(E-Mail Removed)>
>
>> 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)


 
Reply With Quote
 
Alan Johnson
Guest
Posts: n/a
 
      02-14-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What does fstream::getline() return after setting fail bit? Disc Magnet C++ 3 03-05-2010 06:22 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Why does this fail via onSubmit, but work otherwise?? Eric Petzold Javascript 2 06-11-2004 01:57 PM



Advertisments