Simple toString() problem

L

llewelly

This is way off-topic, so please see the mingw faq, and thier mailing
list, at:

http://www.mingw.org/mingwfaq.shtml

I would guess you want gcc-core-3.3.1-20030804-1.tar.gz *and*
gcc-g++-3.3.1-20030804-1.tar.gz . I think those with -src in their
names are source code packages which you need a pre-existing
compiler to compile. (I don't think gcc builds with borland,
however.) g++ indicates a package for the c++ front-end. Note it
won't work without the -core package.

You could also try getting mingw from http://www.bloodshed.net/ ,
which seems a little more user friendly.
<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.
[snip]

It's not a compiler.
 
L

llewelly

Ying Yang said:
Hi,

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

std::string Car::toString()
{
std::eek:stringstream out;
out << "[" << carID << "," << carName << "]";
return out.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.

hm. Ok.

int Car::toString(char* write_to, int len)
{
assert(len > 5);
return snprintf(write_to, len, "[%d,%s]", carID, carName);
}

oh no, oops, C++ doesn't have snprintf. I guess you'll have to live
without it:

int Car::toString2(char* write_to, size_t len)
{
assert(len > numeric_limits<int>::digits
+ strlen(carName) + strlen("[,]") && len < INT_MAX);
return sprintf(write_to, "[%d,%s]", carID, carName);
}

I changed the interface of the toString function because it is better
to put the responsibility of memory allocation clearly in the
hands of the caller than to leave them wondering if they need to
free, delete, or just ignore the pointer returned.

You may want a better way to handle too small buffers.
 
K

Kevin Goodsell

A said:
but i need

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

Read the other replies. You've been given several possible answers.

Frankly, you don't understand the language well enough to be trusted
with char* strings. You should abandon the idea of using pointers and
use std::string instead. You'll save yourself all kinds of trouble, and
you'll save us time explaining why your horribly broken pointer code
doesn't work.

-Kevin
 
K

Kevin Goodsell

Lem said:
I was refering to the array...=)

For the second time, PLEASE don't top-post! Re-read section 5 of the FAQ
for posting guidelines.

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

If you can't be bothered to do this, then please find a different group
to top-post in.

You said: "as str is created on the local stack, it will be
automatically destroyed when the function returns."

I said that's not relevant. And it's not. Yes, local automatic variables
are destroyed when the function returns. But it's still perfectly valid
to return one. Returning a *pointer* to a local automatic variable is
bad, but that isn't what you said.

-Kevin
 
Y

Ying Yang

Hi,

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

std::string Car::toString()
{
std::eek:stringstream out;
out << "[" << carID << "," << carName << "]";
return out.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.

hm. Ok.

int Car::toString(char* write_to, int len)
{
assert(len > 5);
return snprintf(write_to, len, "[%d,%s]", carID, carName);
}

oh no, oops, C++ doesn't have snprintf. I guess you'll have to live
without it:

int Car::toString2(char* write_to, size_t len)
{
assert(len > numeric_limits<int>::digits
+ strlen(carName) + strlen("[,]") && len < INT_MAX);
return sprintf(write_to, "[%d,%s]", carID, carName);
}

I changed the interface of the toString function because it is better
to put the responsibility of memory allocation clearly in the
hands of the caller than to leave them wondering if they need to
free, delete, or just ignore the pointer returned.

You may want a better way to handle too small buffers.

Thanks dude, but i solved my problem by simply using the methods from
<string.h>: strcpy and strcat. It was much more simpler, but I guess the
downfall is the use of a global buffer of a very large size. Anyway, memory
is dirt cheap these days, so no harm done.


wewewe
 
K

Karl Heinz Buchegger

Ying said:
Thanks dude, but i solved my problem by simply using the methods from
<string.h>: strcpy and strcat. It was much more simpler, but I guess the
downfall is the use of a global buffer of a very large size. Anyway, memory
is dirt cheap these days, so no harm done.

That sounds as if there is a problem sitting and waiting in your code.
It waits until it can do the most harm, then it will show up :)
 

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,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top