Solved

Array of objects

Posted on 2003-11-07
5
298 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great work blahpers! Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 127
Focus not getting shifted out of  editbox 2 50
Find Visual Studio Tools 2 77
How to split this in C++ 4 76
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now