Function return string

S

streamkid

Why can't a function not return string? (error: new types may not be
defined in a return type)...
How can i solve it?
I thought of passing the string into the function, but that will make
me redesign most of the app and i want to avoid it.. is there any other
way to overcome this?

tia, streamkid
 
O

Ondra Holub

(e-mail address removed) napsal:
Why can't a function not return string? (error: new types may not be
defined in a return type)...
How can i solve it?
I thought of passing the string into the function, but that will make
me redesign most of the app and i want to avoid it.. is there any other
way to overcome this?

tia, streamkid

You wrote here no code, so it is very hard to think, where is your
problem. But there is definitely possible to return strings:

#include <string>
std::string RetString()
{
return "Hello";
}

If you mean returning C-strings, it works too:
const char* RetCString()
{
return "Hello";
}
 
S

Signal9

Why can't a function not return string? (error: new types may not be
defined in a return type)...
How can i solve it?
I thought of passing the string into the function, but that will make
me redesign most of the app and i want to avoid it.. is there any other
way to overcome this?

tia, streamkid


Please paste a snippet of your code. There is nothing stopping you
from returning a class or a C style string.

Also, please post what compiler and OS you are compiling this on.
 
S

streamkid

Ï/Ç Ondra Holub Ýãñáøå:
(e-mail address removed) napsal:

You wrote here no code, so it is very hard to think, where is your
problem. But there is definitely possible to return strings:

#include <string>
std::string RetString()
{
return "Hello";
}

If you mean returning C-strings, it works too:
const char* RetCString()
{
return "Hello";
}


thanks for the quick answer :)

well i tried this and it works :s
BUT
i have
class a
{
public:
string getit();
private:
string astring;
}

i try to compile it and is says the error mentioned above...
 
I

Ian Collins

Why can't a function not return string? (error: new types may not be
defined in a return type)...
How can i solve it?

Without code, I can only guess that you have forgotten to qualify
'string' with 'std::'.
 
S

streamkid

Ï/Ç Signal9 Ýãñáøå:
Please paste a snippet of your code. There is nothing stopping you
from returning a class or a C style string.

Also, please post what compiler and OS you are compiling this on.

i tried on slackware using g++
and on vista using devcpp
 
O

Ondra Holub

(e-mail address removed) napsal:
Why can't a function not return string? (error: new types may not be
defined in a return type)...
How can i solve it?
I thought of passing the string into the function, but that will make
me redesign most of the app and i want to avoid it.. is there any other
way to overcome this?

tia, streamkid

I think you have defined class without semicolon at the end. Something
like this:

class A
{
} // Here is missing semicolon

int main()
{
}
 
S

streamkid

to both Ondra Holub:
i 've forgot the semicolon only here..

Ian Collins:
no unfortunatelly you haven't. that's not the prob.. i wish it was..
 
I

Ian Collins

to both Ondra Holub:
i 've forgot the semicolon only here..

Ian Collins:
no unfortunatelly you haven't. that's not the prob.. i wish it was..
I haven't what? Please provide context.
 
S

Salt_Peter

thanks for the quick answer :)

well i tried this and it works :s
BUT
i have
class a
{
public:
string getit();
private:
string astring;
}

i try to compile it and is says the error mentioned above...

There is no way you'll get that to compile.
There is no such thing as a string, a std::string maybe and thats only
if you #include <string>.
A class declaration requires a semi-colon.
Any member function you declare needs to be implemented if you plan to
use it.
 
S

streamkid

Ï/Ç Salt_Peter Ýãñáøå:
There is no way you'll get that to compile.
There is no such thing as a string, a std::string maybe and thats only
if you #include <string>.
A class declaration requires a semi-colon.
Any member function you declare needs to be implemented if you plan to
use it.

well that's not the code!!!!!!!!!!!!!!!!!!!!
i just wrote this to show sth _like_ that i've written and i'm trying
to compile!
ofc i'm not trying to compile this!
 
I

Ian Collins

well that's not the code!!!!!!!!!!!!!!!!!!!!
i just wrote this to show sth _like_ that i've written and i'm trying
to compile!
ofc i'm not trying to compile this!
One of the golden rules (and FAQs) for this (and most other language
groups) is "post a compilable example that demonstrates your problem"
 
K

Kai-Uwe Bux

?/? Salt_Peter ??????:

well that's not the code!!!!!!!!!!!!!!!!!!!!

Well, then post the code.
i just wrote this to show sth _like_ that i've written and i'm trying
to compile!

Please understand that we cannot see what you have on your screen; we only
see what you post here. If that has errors, we point them out. If those
errors are not the ones you are interested in, don't put them in your posts
in the first place.
ofc i'm not trying to compile this!

See FAQ 5.8.


Best

Kai-Uwe Bux
 
S

Salt_Peter

Ï/Ç Salt_Peter Ýãñáøå:

well that's not the code!!!!!!!!!!!!!!!!!!!!
i just wrote this to show sth _like_ that i've written and i'm trying
to compile!
ofc i'm not trying to compile this!

Yes it is code. Until proven otherwise, that is.
If you show us code then ... it IS code.
And that WILL NOT COMPILE.
 
M

MC felon

You wrote here no code, so it is very hard to think, where is your
problem. But there is definitely possible to return strings:

#include <string>
std::string RetString()
{
return "Hello";
}

If you mean returning C-strings, it works too:
const char* RetCString()
{
return "Hello";
}

HEYY!!!! where were you when i was shouting myself hoarse about the
same topic. you could have solved my problem a millenium ago....
 
J

Jim Langston

Ï/Ç Signal9 Ýãñáøå:
Please paste a snippet of your code. There is nothing stopping you
from returning a class or a C style string.

Also, please post what compiler and OS you are compiling this on.

i tried on slackware using g++
and on vista using devcpp

Okay, please listen closly.
Post WHAT YOU TRIED TO COMPILE THAT DIDN'T COMPILE.
Other than that, the error is in your program on line 42.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top