473,408 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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)
Feb 13 '07 #1
6 1128
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
Feb 13 '07 #2
On Feb 13, 11:17 am, "The Cool Giraffe" <giraf...@viltersten.com>
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
Feb 13 '07 #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?
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);

Feb 13 '07 #4
On Tue, 13 Feb 2007 17:17:59 +0100 in comp.lang.c++, "The Cool Giraffe"
<gi******@viltersten.comwrote,
>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);

Feb 13 '07 #5
Thomas Tutone wrote/skrev/kaita/popisal/schreibt :
"The Cool Giraffe" <giraf...@viltersten.com>
>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)
Feb 13 '07 #6
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
Feb 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: thecrow | last post by:
I am trying to figure out how to make an object creation fail for ease of error handling. Oddly, I can't work out how to do it. Here's a reduced code sample of how I hoped it would work, but it...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
2
by: John | last post by:
The following code works OK in IE 6.0 but does not work in Netscape 7. The image does not shift when one scrolls down but stays stationary in Netscape. Please help Thank you John function...
44
by: Bamber | last post by:
Why does f get returned ? (compiled with gcc). #include<stdio.h> int func(int a); main() { int f; f=func(7); printf("f=%d",f);
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
6
by: Matt Kowalczyk | last post by:
Hello, Can someone explain to me why the following code works correctly? It would seem like it shouldn't. irc_client.c contains: #include "ircclient.h IRCClient* init_irc_client() {
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.