Solved

Array of objects

Posted on 2003-11-07
5
301 Views
Last Modified: 2012-05-04
The following code gives me an error:

      string* a;
      a = (string*) malloc( sizeof(string) * 10 );
      a[5] = "hehe";

What am I doing wrong?
0
Comment
Question by:daniel_bigham
5 Comments
 
LVL 11

Expert Comment

by:KurtVon
ID: 9703160
You are just making space for the string, you have not actually created a string object.  Try

a[5] = new string;
a[5] = "hehe";

Hope this helps.
0
 
LVL 1

Author Comment

by:daniel_bigham
ID: 9703173
Your suggestion doesn't work, since "new string" returns a pointer to a string, but a[5] is an actual string (not a pointer to a string).

0
 
LVL 9

Expert Comment

by:tinchos
ID: 9703180
the problem is that

string* a; means that a is a pointer and because of that you can access it as a pointer

but you're not allowed to use the operator[] on a pointer................

you can just use the * or -> operators

Apart from that, I guess that the posted code is just too ackward..........

why don't you try with

string a[5];
a[4] = string( "hehe" );

Hope this helps

Tincho
0
 

Accepted Solution

by:
blahpers earned 125 total points
ID: 9703223
All close but not quite.

The poster is trying to dynamically create an array of ten strings and assign the sixth a value of "hehe".

string* a = new string[10];
a[5] = "hehe";
// ... other code here ...
delete [] a;

Of course, STL would discourage you from using pointers--try the following instead:

vector<string> a(10);
a[5] = "hehe";
// ... other code; no need to delete

malloc() and free() have limited usefulness in C++; use them only if necessary, as they circumvent all class mechanisms (such as con-/destructors).
0
 
LVL 1

Author Comment

by:daniel_bigham
ID: 9703242
Great work blahpers! Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question