string as a function return type

Discussion in 'C++' started by C@limero, Oct 3, 2005.

  1. C@limero

    C@limero Guest

    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 ?
     
    C@limero, Oct 3, 2005
    #1
    1. Advertising

  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
    >
    > 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::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 ?


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

    V
     
    Victor Bazarov, Oct 3, 2005
    #2
    1. Advertising

  3. C@limero

    Mike Smith Guest

    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
     
    Mike Smith, Oct 3, 2005
    #3
    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. Greenhorn
    Replies:
    15
    Views:
    849
    Keith Thompson
    Mar 6, 2005
  2. Diego Martins
    Replies:
    5
    Views:
    493
    Diego Martins
    Sep 5, 2006
  3. Luca Forlizzi
    Replies:
    4
    Views:
    473
    Luca Forlizzi
    Nov 14, 2010
  4. andrew cooke
    Replies:
    1
    Views:
    185
    Steven D'Aprano
    Apr 3, 2011
  5. Replies:
    1
    Views:
    199
    Dr.Ruud
    Jul 29, 2006
Loading...

Share This Page