Solved

Array of objects

Posted on 2003-11-07
5
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

696 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