Function return string

Discussion in 'C++' started by streamkid@gmail.com, Dec 9, 2006.

  1. Guest

    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
     
    , Dec 9, 2006
    #1
    1. Advertising

  2. Ondra Holub Guest

    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";
    }
     
    Ondra Holub, Dec 9, 2006
    #2
    1. Advertising

  3. Signal9 Guest

    wrote:
    > 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.
     
    Signal9, Dec 9, 2006
    #3
  4. Guest

    Ï/Ç Ondra Holub Ýãñáøå:
    > 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";
    > }



    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...
     
    , Dec 9, 2006
    #4
  5. Ian Collins Guest

    wrote:
    > 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::'.

    --
    Ian Collins.
     
    Ian Collins, Dec 9, 2006
    #5
  6. Guest

    Ï/Ç Signal9 Ýãñáøå:
    > wrote:
    > > 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.


    i tried on slackware using g++
    and on vista using devcpp
     
    , Dec 9, 2006
    #6
  7. Ian Collins Guest

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


    Looks like I made the correct guess.

    --
    Ian Collins.
     
    Ian Collins, Dec 9, 2006
    #7
  8. Ondra Holub Guest

    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()
    {
    }
     
    Ondra Holub, Dec 9, 2006
    #8
  9. Guest

    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..
     
    , Dec 9, 2006
    #9
  10. Ian Collins Guest

    wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, Dec 9, 2006
    #10
  11. Salt_Peter Guest

    wrote:
    <snip>
    >
    > 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.
     
    Salt_Peter, Dec 9, 2006
    #11
  12. Guest

    Ï/Ç Salt_Peter Ýãñáøå:
    > wrote:
    > <snip>
    > >
    > > 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.


    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!
     
    , Dec 9, 2006
    #12
  13. Ian Collins Guest

    wrote:
    >
    > 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"

    --
    Ian Collins.
     
    Ian Collins, Dec 9, 2006
    #13
  14. Kai-Uwe Bux Guest

    wrote:

    >
    > ?/? Salt_Peter ??????:
    >> wrote:
    >> <snip>
    >> >
    >> > 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.

    >
    > 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
     
    Kai-Uwe Bux, Dec 9, 2006
    #14
  15. Salt_Peter Guest

    wrote:
    > Ï/Ç Salt_Peter Ýãñáøå:
    > > wrote:
    > > <snip>
    > > >
    > > > 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.

    >
    > 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.
     
    Salt_Peter, Dec 9, 2006
    #15
  16. Guest

    ok sorry guys.. :(
     
    , Dec 9, 2006
    #16
  17. MC felon Guest


    >
    > 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....
     
    MC felon, Dec 10, 2006
    #17
  18. Jim Langston Guest

    <> wrote in message
    news:...

    Ï/Ç Signal9 Ýãñáøå:
    > wrote:
    > > 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.


    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.
     
    Jim Langston, Dec 11, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Seong-Kook Shin
    Replies:
    1
    Views:
    495
    Richard Bos
    Jun 18, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    833
    Keith Thompson
    Mar 6, 2005
  3. Replies:
    4
    Views:
    552
    James Kanze
    Jul 22, 2008
  4. Xeno Campanoli
    Replies:
    0
    Views:
    229
    Xeno Campanoli
    Feb 13, 2006
  5. Replies:
    1
    Views:
    196
    Dr.Ruud
    Jul 29, 2006
Loading...

Share This Page