[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Array of objects

Posted on 2003-11-07
5
Medium Priority
?
306 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 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

649 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