Simple toString() problem

Discussion in 'C++' started by Ying Yang, Sep 7, 2003.

  1. Ying Yang

    Ying Yang Guest

    Hi,

    <code snippet>
    char* Car::toString()
    {
    //Example [XR, Holden]
    char* str = {"[", carID, ",", carName, "]"};
    return str;
    }

    Note: carID and carName are private variables of class Car

    Compile Error: Car.cpp initializer for scalar variable requires one element

    Basically, the toString() method doesn't work. Any help appreciated. Note,
    I'm not interested in working with the newer string data type because I want
    to learnto do it with pointers to char.


    Regards
    wewewe
     
    Ying Yang, Sep 7, 2003
    #1
    1. Advertising

  2. Ying Yang wrote:

    > Hi,
    >
    > <code snippet>
    > char* Car::toString()
    > {
    > //Example [XR, Holden]
    > char* str = {"[", carID, ",", carName, "]"};


    The way I see it, you want to return something like:

    "[carId,carName]"

    with the real values of carId and carName, of course. I assume carId and
    carName are also char*.

    Well,
    1) {"[", carID, ",", carName, "]"} is basically the syntax for an array of
    stuff (presumably char* if carId and carName are also char*). In other
    words, if we call it str, its contents are:
    str[0] == "{"
    str[1] == carId
    str[2] == ","
    str[3] == carName
    str[4] == "]"

    Anyway, its type is char*[5] not char*, which is why you are getting the
    error. You are trying to assign an array to a scalar variable.

    2) How you could do it with old-styled stuff:
    //first you would need a buffer:
    char * buf = new char[30]; //let's say a 30 character buffer
    //then fill the buffer
    sprintf(buf,"[%s,%s]", carId, carName); //sprintf requires #include <cstdio>
    return buf;


    Notice that I allocated buf with new, otherwise it would be destroyed as
    soon as toString() exits and the returned pointer would point to nowhere.

    I think you need a good C++ book. See the FAQ.
     
    Dimitris Kamenopoulos, Sep 7, 2003
    #2
    1. Advertising

  3. Ying Yang

    Ying Yang Guest


    > > Hi,
    > >
    > > <code snippet>
    > > char* Car::toString()
    > > {
    > > //Example [XR, Holden]
    > > char* str = {"[", carID, ",", carName, "]"};
    > > return str;
    > > }
    > >
    > > Note: carID and carName are private variables of class Car

    >
    > Use an ostringstream, and switch to using std::string for the return

    value.
    >
    > ostringstream oss;
    > oss << '[' << carID << ',' << carName << ']'; // just like using cout
    > return oss.str();
    >
    > If you /must/ use C-style strings, you're going to have to tackle dynamic
    > memory management. That's a whole 'nother discussion, though.
    >
    > Josh


    What do i have to include to use ostringstream oss?? doesn't not seem like a
    normal way of doing it with pointers to char.

    wewewe
     
    Ying Yang, Sep 7, 2003
    #3
  4. On Sun, 07 Sep 2003 23:37:44 +0930, Ying Yang wrote:

    >
    >> > Hi,
    >> >
    >> > <code snippet>
    >> > char* Car::toString()
    >> > {
    >> > //Example [XR, Holden]
    >> > char* str = {"[", carID, ",", carName, "]"};
    >> > return str;
    >> > }
    >> >
    >> > Note: carID and carName are private variables of class Car

    >>
    >> Use an ostringstream, and switch to using std::string for the return

    > value.
    >>
    >> ostringstream oss;
    >> oss << '[' << carID << ',' << carName << ']'; // just like using cout
    >> return oss.str();
    >>
    >> If you /must/ use C-style strings, you're going to have to tackle dynamic
    >> memory management. That's a whole 'nother discussion, though.
    >>
    >> Josh

    >
    > What do i have to include to use ostringstream oss??


    <sstream>.

    > doesn't not seem like a
    > normal way of doing it with pointers to char.


    No, it uses C++-style strings, which is why I said to switch your return
    type to std::string. Pointers to char is not the normal way, it is the C
    way. In a C++ program, you should use std::string.

    Josh
     
    Josh Sebastian, Sep 7, 2003
    #4
  5. Ying Yang

    Jon Bell Guest

    In article <bjfds8$12du$>,
    Dimitris Kamenopoulos <> wrote:
    >
    >2) How you could do it with old-styled stuff:
    >//first you would need a buffer:
    >char * buf = new char[30]; //let's say a 30 character buffer
    >//then fill the buffer
    >sprintf(buf,"[%s,%s]", carId, carName); //sprintf requires #include <cstdio>
    >return buf;
    >
    >
    >Notice that I allocated buf with new, otherwise it would be destroyed as
    >soon as toString() exits and the returned pointer would point to nowhere.


    And of course you need to make sure to deallocate the memory at some point
    using 'delete', otherwise your program has a memory leak.

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Sep 7, 2003
    #5
  6. "Ying Yang" <> wrote in message
    news:...
    > Hi,
    >
    > <code snippet>
    > char* Car::toString()
    > {
    > //Example [XR, Holden]
    > char* str = {"[", carID, ",", carName, "]"};
    > return str;
    > }
    >
    > Note: carID and carName are private variables of class Car
    >


    Here is a simple way to do what you want, but you will need to delete the
    memory allocated by your toString() externally.

    char* Car::toString()
    {
    char *tmpString = new char[255]; // for example some large buffer to hold
    info.
    sprintf(tmpString, "[%ld,%s], carID, carName);
    return tmpString;
    }

    int main()
    {
    Car car;
    ...
    char *str = car.toString();
    cout << str << endl;
    delete str; // important to delete what toString() allocated.
    }

    Now you could always use std::string() to do this.

    std::string Car::toString()
    {
    std::eek:stringstream ss;
    ss << "[" << cardID << "," << carName << "]";
    return ss.str();
    }

    int main()
    {
    Car car;
    ...

    // no messy dealing with or deleting pointers.
    //
    cout << car.toString() << endl;
    }

    The important thing to remember is that you were declaring a pointer to a
    char in you toString() method, which is local to that method. Once the
    method returns that pointer is no longer valid, nor is the memory to which
    it is pointing. You need to allocate and return, which can be done in the
    first example the C way or in the second example, managed by objects, the
    C++ way.

    Tom
     
    Thomas J. Clancy, Sep 7, 2003
    #6
  7. Ying Yang

    Lem Guest

    1) you cannot assign char * variables this way
    2)as str is created on the local stack, it will be automatically destroyed
    when the function returns.
    Hope this helps

    Lem


    "Ying Yang" <> wrote in message
    news:...
    > Hi,
    >
    > <code snippet>
    > char* Car::toString()
    > {
    > //Example [XR, Holden]
    > char* str = {"[", carID, ",", carName, "]"};
    > return str;
    > }
    >
    > Note: carID and carName are private variables of class Car
    >
    > Compile Error: Car.cpp initializer for scalar variable requires one

    element
    >
    > Basically, the toString() method doesn't work. Any help appreciated. Note,
    > I'm not interested in working with the newer string data type because I

    want
    > to learnto do it with pointers to char.
    >
    >
    > Regards
    > wewewe
    >
    >
    >
    >
    >
    >
    >
     
    Lem, Sep 8, 2003
    #7
  8. Ying Yang

    A Guest

    "Josh Sebastian" <> wrote in message
    news:p...
    > On Sun, 07 Sep 2003 23:37:44 +0930, Ying Yang wrote:
    >
    > >
    > >> > Hi,
    > >> >
    > >> > <code snippet>
    > >> > char* Car::toString()
    > >> > {
    > >> > //Example [XR, Holden]
    > >> > char* str = {"[", carID, ",", carName, "]"};
    > >> > return str;
    > >> > }
    > >> >
    > >> > Note: carID and carName are private variables of class Car
    > >>
    > >> Use an ostringstream, and switch to using std::string for the return

    > > value.
    > >>
    > >> ostringstream oss;
    > >> oss << '[' << carID << ',' << carName << ']'; // just like using cout
    > >> return oss.str();
    > >>
    > >> If you /must/ use C-style strings, you're going to have to tackle

    dynamic
    > >> memory management. That's a whole 'nother discussion, though.
    > >>
    > >> Josh

    > >
    > > What do i have to include to use ostringstream oss??

    >
    > <sstream>.
    >
    > > doesn't not seem like a
    > > normal way of doing it with pointers to char.

    >
    > No, it uses C++-style strings, which is why I said to switch your return
    > type to std::string. Pointers to char is not the normal way, it is the C
    > way. In a C++ program, you should use std::string.
    >
    > Josh


    Just found out that Dev c++ 4.0 doesn't have the header file name
    <sstream> - great!

    wewewe


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
     
    A, Sep 8, 2003
    #8
  9. Lem wrote:

    Please do not top-post. Re-read section 5 of the FAQ for posting guidelines.

    http://www.parashift.com/c -faq-lite/

    > 1) you cannot assign char * variables this way
    > 2)as str is created on the local stack, it will be automatically destroyed
    > when the function returns.


    That's not necessarily relevant. This is perfectly legal:

    char *func()
    {
    char *p = "some string";
    return p;
    }

    The fact that p goes out of scope does not make the returned value
    invalid. If the thing it pointed to was also going out of scope, then
    there'd be a problem.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 8, 2003
    #9
  10. Ying Yang

    A Guest


    > Ying Yang wrote:
    >
    > > Hi,
    > >
    > > <code snippet>
    > > char* Car::toString()
    > > {
    > > //Example [XR, Holden]
    > > char* str = {"[", carID, ",", carName, "]"};

    >
    > The way I see it, you want to return something like:
    >
    > "[carId,carName]"
    >
    > with the real values of carId and carName, of course. I assume carId and
    > carName are also char*.
    >
    > Well,
    > 1) {"[", carID, ",", carName, "]"} is basically the syntax for an array of
    > stuff (presumably char* if carId and carName are also char*). In other
    > words, if we call it str, its contents are:
    > str[0] == "{"
    > str[1] == carId
    > str[2] == ","
    > str[3] == carName
    > str[4] == "]"
    >
    > Anyway, its type is char*[5] not char*, which is why you are getting the
    > error. You are trying to assign an array to a scalar variable.


    Here you are creating an array of characters, and the compiler will not like
    it since it's expecting you to return a pointer to a char (char*). Any
    suggestions?


    > 2) How you could do it with old-styled stuff:
    > //first you would need a buffer:
    > char * buf = new char[30]; //let's say a 30 character buffer
    > //then fill the buffer
    > sprintf(buf,"[%s,%s]", carId, carName); //sprintf requires #include

    <cstdio>
    > return buf;


    I need something that will allow concatentation as I may have more than one
    car to print out, and I don't think sprintf will do concatenation of
    buffers.

    wewewe


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
     
    A, Sep 8, 2003
    #10
  11. Ying Yang

    A Guest

    "Kevin Goodsell" <> wrote in message
    news:feV6b.3350$...
    > Lem wrote:
    >
    > Please do not top-post. Re-read section 5 of the FAQ for posting

    guidelines.
    >
    > http://www.parashift.com/c -faq-lite/
    >
    > > 1) you cannot assign char * variables this way
    > > 2)as str is created on the local stack, it will be automatically

    destroyed
    > > when the function returns.

    >
    > That's not necessarily relevant. This is perfectly legal:
    >
    > char *func()
    > {
    > char *p = "some string";
    > return p;
    > }


    but i need

    char *p = "some string" + carID + carName; // but this gives error. what to
    do?

    wewewe



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
     
    A, Sep 8, 2003
    #11
  12. A wrote:


    > Here you are creating an array of characters, and the compiler will not
    > like it since it's expecting you to return a pointer to a char (char*).
    > Any suggestions?


    I suppose you mean the line
    char * buf = new char[30];

    Yes, I am creating an array of pointers but buf points to its beginning.
    Perfectly OK. In any case, the distinction between a char* and a char[] (in
    general a T* and a T[]) is not always very clear. IIRC the C (and probably
    the C++) FAQ explains when you can use a pointer as an array and vice
    versa.

    >> 2) How you could do it with old-styled stuff:
    >> //first you would need a buffer:
    >> char * buf = new char[30]; //let's say a 30 character buffer
    >> //then fill the buffer
    >> sprintf(buf,"[%s,%s]", carId, carName); //sprintf requires #include

    > <cstdio>
    >> return buf;

    >
    > I need something that will allow concatentation as I may have more than
    > one car to print out, and I don't think sprintf will do concatenation of
    > buffers.


    But it will:
    sprintf(concatenated_buf, "%s%s", buf1,buf2);

    provided concatenated_buf points to an array large enough to store the
    characters of buf1 and buf2 and a trailing '\0'. Anyway, if you have to
    print many cars, why should you first put the WHOLE output into one buffer?
    Just print them one by one:

    char * buf;
    for (/*every car*/){
    buf = car.toString();
    printf("%s\n",buf);
    delete[] buf; //remember it was allocated with new[]
    }
     
    Dimitris Kamenopoulos, Sep 8, 2003
    #12
  13. "A" <> wrote in message news:<>...

    > Just found out that Dev c++ 4.0 doesn't have the header file name
    > <sstream> - great!


    Compiler-specific issues are off-topic here, but since I happen to
    know a little bit about this one:

    Dev-C++ 4 comes with the Mingw32 port of GCC 2.95.2 for Windows.
    <sstream> is available as a separate download for that version of GCC:

    <http://gcc.gnu.org/fom_serv/cache/80.html>

    Note that GCC 2.95.2 is almost four years old, whereas GCC 3.3.1 was
    released last month and comes with the latest Mingw32 release,
    available at <http://www.mingw.org/>. Perhaps an upgrade is in order.
    :)

    - Shane
     
    Shane Beasley, Sep 8, 2003
    #13
  14. Ying Yang

    Lem Guest

    I was refering to the array...=)

    Lem

    "Kevin Goodsell" <> wrote in message
    news:feV6b.3350$...
    > Lem wrote:
    >
    > Please do not top-post. Re-read section 5 of the FAQ for posting

    guidelines.
    >
    > http://www.parashift.com/c -faq-lite/
    >
    > > 1) you cannot assign char * variables this way
    > > 2)as str is created on the local stack, it will be automatically

    destroyed
    > > when the function returns.

    >
    > That's not necessarily relevant. This is perfectly legal:
    >
    > char *func()
    > {
    > char *p = "some string";
    > return p;
    > }
    >
    > The fact that p goes out of scope does not make the returned value
    > invalid. If the thing it pointed to was also going out of scope, then
    > there'd be a problem.
    >
    > -Kevin
    > --
    > My email address is valid, but changes periodically.
    > To contact me please use the address from a recent posting.
    >
     
    Lem, Sep 8, 2003
    #14
  15. Ying Yang

    Agent Mulder Guest

    <Shane Beasley>
    > Note that GCC 2.95.2 is almost four years old, whereas GCC 3.3.1 was
    > released last month and comes with the latest Mingw32 release,
    > available at <http://www.mingw.org/>. Perhaps an upgrade is in order.

    </Shane Beasley>

    I can't figure it out on the download page. Do I need:

    gcc-ada-3.3.1-20030804-1.tar.gz 12461 kb Aug 07, 2003 21:02
    gcc-core-3.3.1-20030804-1-src.tar.gz 12958 kb Aug 07, 2003 19:11
    gcc-core-3.3.1-20030804-1.tar.gz 2220 kb Aug 07, 2003 19:26
    gcc-g++-3.3.1-20030804-1-src.tar.gz 2660 kb Aug 07, 2003 16:04
    gcc-g++-3.3.1-20030804-1.tar.gz 2169 kb Aug 07, 2003 19:38
    gcc-g77-3.3.1-20030804-1-src.tar.gz 1451 kb Aug 07, 2003 15:52
    gcc-g77-3.3.1-20030804-1.tar.gz 1696 kb Aug 07, 2003 19:53
    gcc-java-3.3.1-20030804-1-src.tar.gz

    or one of the many more others? My platform is WindowsME. I work
    with Borland 5.5.1 now. I expect a file C++Win32.zip or something like
    it but can't find it. Can someone help me out? Thank you.

    -X
     
    Agent Mulder, Sep 8, 2003
    #15
  16. > I can't figure it out on the download page. Do I need:
    >
    > gcc-ada-3.3.1-20030804-1.tar.gz 12461 kb Aug 07, 2003 21:02
    > gcc-core-3.3.1-20030804-1-src.tar.gz 12958 kb Aug 07, 2003 19:11
    > gcc-core-3.3.1-20030804-1.tar.gz 2220 kb Aug 07, 2003 19:26
    > gcc-g++-3.3.1-20030804-1-src.tar.gz 2660 kb Aug 07, 2003 16:04
    > gcc-g++-3.3.1-20030804-1.tar.gz 2169 kb Aug 07, 2003 19:38
    > gcc-g77-3.3.1-20030804-1-src.tar.gz 1451 kb Aug 07, 2003 15:52
    > gcc-g77-3.3.1-20030804-1.tar.gz 1696 kb Aug 07, 2003 19:53
    > gcc-java-3.3.1-20030804-1-src.tar.gz
    >
    > or one of the many more others? My platform is WindowsME. I work
    > with Borland 5.5.1 now. I expect a file C++Win32.zip or something like
    > it but can't find it. Can someone help me out? Thank you.


    Read the download page http://www.mingw.org/download.shtml, it explains
    what you need to download and how to install.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Sep 8, 2003
    #16
  17. Ying Yang

    Agent Mulder Guest

    "Peter van Merkerk" <> wrote in message
    news:bjhptt$j015t$-berlin.de...
    > I can't figure it out on the download page. Do I need:
    >
    > gcc-ada-3.3.1-20030804-1.tar.gz 12461 kb Aug 07, 2003 21:02
    > gcc-core-3.3.1-20030804-1-src.tar.gz 12958 kb Aug 07, 2003 19:11
    > gcc-core-3.3.1-20030804-1.tar.gz 2220 kb Aug 07, 2003 19:26
    > gcc-g++-3.3.1-20030804-1-src.tar.gz 2660 kb Aug 07, 2003 16:04
    > gcc-g++-3.3.1-20030804-1.tar.gz 2169 kb Aug 07, 2003 19:38
    > gcc-g77-3.3.1-20030804-1-src.tar.gz 1451 kb Aug 07, 2003 15:52
    > gcc-g77-3.3.1-20030804-1.tar.gz 1696 kb Aug 07, 2003 19:53
    > gcc-java-3.3.1-20030804-1-src.tar.gz
    >
    > or one of the many more others? My platform is WindowsME. I work
    > with Borland 5.5.1 now. I expect a file C++Win32.zip or something like
    > it but can't find it. Can someone help me out? Thank you.


    <Peter van Merkerk>
    Read the download page http://www.mingw.org/download.shtml, it explains
    what you need to download and how to install.
    <Peter van Merkerk>

    That's where I got that confusing list from. I think MinGW-3.0.0-1.exe
    is a candidate. Or do I get lured into some IDE then? ;-)

    "I have nothing against IDEs. I think they're great" ---Charles Petzold

    -X

    http://prdownloads.sf.net/mingw/MinGW-3.0.0-1.exe?download
     
    Agent Mulder, Sep 8, 2003
    #17
  18. > <Peter van Merkerk>
    > Read the download page http://www.mingw.org/download.shtml, it

    explains
    > what you need to download and how to install.
    > <Peter van Merkerk>
    >
    > That's where I got that confusing list from.


    Besides the list there is also an explanation of the items in the list.

    > I think MinGW-3.0.0-1.exe
    > is a candidate. Or do I get lured into some IDE then? ;-)


    No need to worry, the MinGW site does not distribute IDE's.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Sep 8, 2003
    #18
  19. Ying Yang

    Dirk Feytons Guest

    Dirk Feytons, Sep 8, 2003
    #19
  20. Ying Yang

    Ying Yang Guest

    "Dirk Feytons" <> wrote in message
    news:...
    > A wrote:
    >
    >
    > > Just found out that Dev c++ 4.0 doesn't have the header file name
    > > <sstream> - great!

    >
    > Download and install Dev-C++ v4.9.8.0 from
    > http://sourceforge.net/projects/dev-cpp
    > After that update to v4.9.8.1 using the built-in update tool.
    > This version of Dev-C++ uses GCC v3.2.


    but v4.9.8.1 is in beta stage, and yes I have tried it and had alot of
    strange compile-time errors when developing.

    I think i will just download the missing header files from somewhere and put
    it in the system header directory.


    wewewe


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.510 / Virus Database: 307 - Release Date: 15/08/2003
     
    Ying Yang, Sep 8, 2003
    #20
    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. Mark Kamoski
    Replies:
    3
    Views:
    15,751
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Jimmy [Used-Disks]

    DateTime.ToString() format question

    Jimmy [Used-Disks], Sep 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    5,356
    Jimmy [Used-Disks]
    Sep 5, 2003
  3. Norman Yuan
    Replies:
    0
    Views:
    4,829
    Norman Yuan
    Jan 26, 2006
  4. Chen Yang

    toString align problem

    Chen Yang, Nov 4, 2003, in forum: Java
    Replies:
    7
    Views:
    392
    DrAcKe
    Nov 4, 2003
  5. Fergus McMenemie

    LibXML element->toString vs document->toString

    Fergus McMenemie, Jul 12, 2012, in forum: Perl Misc
    Replies:
    5
    Views:
    632
    Fergus McMenemie
    Jul 17, 2012
Loading...

Share This Page