Y
Ying Yang
I have solved my problem.
Thanks to all that replied.
wewewewe
Thanks to all that replied.
wewewewe
[snip]<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.
Ying Yang said: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.
A said:but i need
char *p = "some string" + carID + carName; // but this gives error. what to
do?
Lem said:I was refering to the array...=)
Hi,
<code snippet>
char* Car::toString()
{
//Example [XR, Holden]
char* str = {"[", carID, ",", carName, "]"};
return str;
}
std::string Car::toString()
{
std: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.
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.
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.