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

T

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));



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

Rolf Magnus

The said:
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);
 
V

Victor Bazarov

The said:
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
 
T

Thomas Tutone

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
 
D

David Harmon

On Tue, 13 Feb 2007 17:17:59 +0100 in comp.lang.c++, "The Cool Giraffe"
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);
 
T

The Cool Giraffe

Thomas Tutone wrote/skrev/kaita/popisal/schreibt :
change that to:

return std::string(ch);

and all should be good.

Best regards,


Got it. Thanks to all.
 
A

Alan Johnson

Rolf said:
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."
 

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

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top