Solved

new'ing an array of classes.

Posted on 1998-05-11
7
215 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
Comment Utility
It should be

classArray[i].MemberFunction();

or

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

Expert Comment

by:shash
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:spearhead
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
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…

762 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

12 Experts available now in Live!

Get 1:1 Help Now