473,503 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string as a function return type

C
If I want to declare a function in a header file that returns a
string, I always get an error (something complaining about
a syntax error before an ')').

I have declared:

#ifndef MYHEADER_H
#define MYHEADER_H

string function(int day, int month, int year);

#endif

But then I get always an error from the compiler.

If I change this to

#ifndef MYHEADER_H
#define MYHEADER_H

const char* function(int day, int month, int year);

#endif

I get no error.

But the problem then is, since I use an std::ostringstream oss in
the definition, I have to write "ugly" things like

return oss.str().c_str();

Is there a way to bypass this ?
Oct 3 '05 #1
2 1520
C@limero wrote:
If I want to declare a function in a header file that returns a
string, I always get an error (something complaining about
a syntax error before an ')').

I have declared:

#ifndef MYHEADER_H
#define MYHEADER_H

string function(int day, int month, int year);
'string' is a typedef declared in <string> header. You need to include
that header before using 'string'. And use 'std::string' because the
name 'string' (the typedef) is declared in the 'std' namespace. So, the
line above should be replaced with two:

#include <string>
std::string function(int day, int month, int year);

#endif

But then I get always an error from the compiler.

If I change this to

#ifndef MYHEADER_H
#define MYHEADER_H

const char* function(int day, int month, int year);

#endif

I get no error.

But the problem then is, since I use an std::ostringstream oss in
the definition, I have to write "ugly" things like

return oss.str().c_str();

Is there a way to bypass this ?


No, there is no way to bypass it. It has to be resolved.

V
Oct 3 '05 #2
C@limero wrote:
If I want to declare a function in a header file that returns a
string, I always get an error (something complaining about
a syntax error before an ')').

I have declared:

#ifndef MYHEADER_H
#define MYHEADER_H
#include <string>
std::string function(int day, int month, int year);
#endif


string is not a built-in type; it is a class, declared in header
<string>, and in namespace std.

--
Mike Smith
Oct 3 '05 #3

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

Similar topics

9
3668
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
9
8686
by: David Carter-Hitchin | last post by:
Hi, I'm not very experienced with c++ so I'm sure this is something obvious that is easily solved, but for the life of me I can't seem to figure it out. I'd be very grateful for some...
1
1831
by: Neil Schemenauer | last post by:
The title is perhaps a little too grandiose but it's the best I could think of. The change is really not large. Personally, I would be happy enough if only %s was changed and the built-in was...
4
5371
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
4
8775
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
6
5404
by: tshad | last post by:
The error I am getting is: ******************************************************************* Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not...
18
2240
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
6
7593
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
4
2360
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript....
3
5077
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
0
7202
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
7086
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...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
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
5578
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,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.