Simple toString() problem

Y

Ying Yang

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
 
D

Dimitris Kamenopoulos

Ying said:
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.
 
Y

Ying Yang

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
 
J

Josh Sebastian

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.

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
 
J

Jon Bell

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.
 
T

Thomas J. Clancy

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

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
 
L

Lem

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
 
A

A

Josh Sebastian 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

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.

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
 
K

Kevin Goodsell

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
 
A

A

Ying said:
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
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
 
A

A

Kevin Goodsell said:
Lem wrote:

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

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
 
D

Dimitris Kamenopoulos

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
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[]
}
 
S

Shane Beasley

A said:
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
 
L

Lem

I was refering to the array...=)

Lem

Kevin Goodsell said:
Lem wrote:

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

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
 
A

Agent Mulder

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
 
P

Peter van Merkerk

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.
 
A

Agent Mulder

Peter van Merkerk said:
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
 
P

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.
 
Y

Ying Yang

Dirk Feytons said:
A wrote:



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
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top