Solved

new'ing an array of classes.

Posted on 1998-05-11
7
220 Views
Last Modified: 2013-12-14
Is this even possible?  Eg:
float *fp = new float[10];  // allocate an array of type float

class NewClass
{
...
}

NewClass *classArray = new NewClass[10];    // Is this possible?

Compiling and running this in the debugger MS VC++ 5 shows that the last line news a single object of class NewClass and the trailing [10] seems to have been dispensed with.  Trying to access a member function of classArray[0] produces a compile error.

classArray[0]->MemberFunction();

F:\Espro For Windows\Source\matworksht.cpp(152) : error C2819: type 'ClassArray' does not have an overloaded member 'operator ->'
F:\Espro For Windows\Source\matworksht.cpp(152) : error C2227: left of '->MemberFunction' must point to class/struct/union

0
Comment
Question by:spearhead
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1175853
It should be

classArray[i].MemberFunction();

or

(classArray + i)->MemberFunction();
0
 

Expert Comment

by:shash
ID: 1175854
In the statement,

classArray[0]->MemberFunction();

the moment u use classArray[0], it means *(classArray + 0). In other words the classArray pointer has been dereferenced. So the right way to invoke the MemberFunction() is

classArray[0].MemberFunction();

The error, "No overloaded member 'operator ->'" is correct but is misleading.
0
 

Expert Comment

by:shash
ID: 1175855
This is what Sun C++ 4.2 compiler has to say about the error.

"Error: Pointer type needed instead of NewClass"

which ofcourse is more helpful than the one by MSVC++ compiler.

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 

Author Comment

by:spearhead
ID: 1175856
shash

I was actually guessing at a minor problem with the MSVC++ compiler as there is no real difference between classArray[i] and (classArray + i) as it is all just pointer arithmetic.  The [] notation is (IMO) a lot easier to read and therefore maintain so I have elected to go with that.  The MSVC++ error message sent me down the wrong track when trying to interpret the problem.

Is there a way to split the points on this one?  I don't really want to fail chensu as his answer was (technically) correct however your solution appears to be a better one.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1175857
shash .. the VC messages actually cover mode cases.

There would be nothing wrong with
  classArray[0]->MemberFunction();
IF NewClass has an operator-> defined.  For exmaple, NewClass might be a smart pointer class.  In this case, one assumes that NewClass does NOT have an operator->, hence the first error message.

After determining that there is no operator-> override, VC is assuming that you mean the default ->.  The second message VC reports says that the LHS of the -> needs to be a pointer to a struct, class or union.  Which is also exactly right.

So the two message taken together cover both cases and are not at all misleading when you appreciate the two potential problems here.

If your problem was that you had forgotten the operator->, then the first message would be very helpful indeed.

0
 

Author Comment

by:spearhead
ID: 1175858
Uh Oh!  Now I am not sure which answer is the most correct.  No, NewClass is NOT a smart pointer class so on allocation, classArray (and hence classArray[0]) should simply be a pointer to the initial object instantiated.

(classArray + 1)->MemberFunction()
as suggested by chensu, and so does
classArray[1].MemberFunction()
which, after thinking about it, I should have known that classArray[1] would not be a pointer, but an object in much the same way as string[1] is a char even though tring may be a char *.

I will award the points to the original answer suggested and hopefully this is not seen as unfair with regard to shash 's answer

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1175859
spearhead .. my comments were to explain what the error message you received meant, as shash claimed they weren't helpful.

We are all saying the same thing .. just adding to the explaination.

you just used a '->' where you should have used a '.'

Its as simple as that

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
The line on IDE 4 91
gdb doesn't stop on breakpoint 2 95
Syntax Error 2 77
How to determine if the result of an equation is an integer in C++? 3 19
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
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…

831 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