Solved

new'ing an array of classes.

Posted on 1998-05-11
7
216 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Compile GLUT with Visual Studio 2015 1 108
convert char array to number in c 5 83
Change local server setting in php 6 81
Arduino EDI - Programming language 3 80
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. …
  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 …
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…
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.

910 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

20 Experts available now in Live!

Get 1:1 Help Now