string as a return type

C

C@limero

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::eek:stringstream oss in
the definition, I have to write "ugly" things like

return oss.str().c_str();

Is there a way to bypass this ?
 
V

Victor Bazarov

C@limero said:
If I want to declare a function in [...]

This is not a chat room. Post once and wait for the replies.

Seven minutes, ten minutes, half-an-hour, whatever it takes for
the message to propagate to our servers, then for us to reply,
then for our reply to propagate back to your server, you just
have to sit there and wait. If it takes more than a day, then
you might post again, but you need to justify posting again by,
say, claiming to have not received any replies within 24 hours.

OK?
 
B

bkgomez

Hard to tell without seeing the actual error messeges and maybe the
full header. But the first thing that comes to mind is including the
line:

using namespace std;

after the includes.
 
A

Alf P. Steinbach

* (e-mail address removed):
Hard to tell without seeing the actual error messeges and maybe the
full header. But the first thing that comes to mind is including the
line:

using namespace std;

after the includes.

Don't _ever_ use that in a header.
 
P

Peter_Julian

| Hard to tell without seeing the actual error messeges and maybe the
| full header. But the first thing that comes to mind is including the
| line:
|
| using namespace std;
|
| after the includes.
|

using namespace std;

does not belong in a header.

// s.h
#ifndef S_H_
#define S_H_

#include <string>

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

#endif
___

// s.cpp
#include "s.h"
using std::string // much better than using namespace std;

string function(int day, int month, int year)
{
...
}
___

A quick scan of the source's using directives is instant documentation
about the interface and its implementation. Hence, even in the source
using namespace std has its shortcomings.
 
C

C@limero

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::eek:stringstream oss in
the definition, I have to write "ugly" things like

return oss.str().c_str();

Is there a way to bypass this ?

Thanks to everybody.
Sorry for posting twice, but I had problems with my newsserver. :-(
 
B

benben

C@limero said:
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::eek:stringstream oss in
the definition, I have to write "ugly" things like

return oss.str().c_str();

oss.str() creates a temporary string object on the stack;
..c_str() returns a c style const string from the temporary object;
The c style const string is managed by the temporary string object;
before you can get away with the line the temp string object is destroyed;
and the c style string is invalidated;
and so if you try to use the c style string;
the behavior should be undefined.
 

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,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top