473,508 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array of classes

Hi, I'm hoping someone can help me out.

If I declare a class, eg.

class CSomeclass
{
public:
var/func etc.....
private
varfunc etc..
};

Then in the code I create a pointer to this class definition

CSomeClass *MyClass;

I want to be able to dynamically increase the size of the
class as I go. Imagine I have a current count of the
object - eg. Count. Whenever I try to allocate
memory for it,

Count++;
MyClass[Count] = new SomeClass;

But it throws an error:
error C2679: binary '=' : no operator found which takes
a right-hand operand of type 'CAlien *'
(or there is no acceptable conversion)

I have no idea how many instances I am going to need ahead of time,
and I can't find and combination of the line that allows it to work.
If anyone has any advicce on what I'm doing wrong it would be much
apreciated. Thank you.

James Alger
Jul 22 '05 #1
2 3313
On 2 May 2004 10:16:57 -0700, ja***********@hotmail.com (James) wrote:
Hi, I'm hoping someone can help me out.

If I declare a class, eg.

class CSomeclass
{
public:
var/func etc.....
private
varfunc etc..
};

Then in the code I create a pointer to this class definition

CSomeClass *MyClass;
First of all, let's make sure you know what you've got here. The way you've
defined it, MyClass is a pointer to a CSomeClass objet (or an array of
them), but the objects don't yet exist. In order for it to actually point
to an array of objects of some given size n, you'd have to set things up as
follows:

MyClass = new CSomeClass[n];

The size 'n' is fixed at that point, and all of the objects get default
initialized.

I want to be able to dynamically increase the size of the
class as I go. Imagine I have a current count of the
object - eg. Count. Whenever I try to allocate
memory for it,

Count++;
MyClass[Count] = new SomeClass;
(Did you mean CSomeClass rather than SomeClass?)

Two things wrong here. First, your "Count" variable can be used to keep
track of how many items in an existing array you've actually used, but that
doesn't affect the size of the array. Second, your assignment statement is
treating elements of the MyClass dynamic array as if they were each a
pointer. This may in fact be what you want, but then the definition of
MyClass would have had to be:

CSomeClass **MyClass;

and you'd have had to initialize it by creating a dynamic array of
/pointers/ to CSomeClass, e.g.:

CSomeClass = new CSomeClass *[n];

Then you can dynamically create new CSomeClass objects (using 'new' as you
did) and gradually fill in the array of pointers. If you know the upper
maximum for the number you need, and it isn't outrageously high, then
choosing 'n' above to be large enough would give you a fairly efficient
implementation. The elements you never used wouldn't cost you much (just a
pointer's worth of memory in the dynamic array).

But it throws an error:
error C2679: binary '=' : no operator found which takes
a right-hand operand of type 'CAlien *'
(or there is no acceptable conversion)

I have no idea how many instances I am going to need ahead of time,
and I can't find and combination of the line that allows it to work.
If anyone has any advicce on what I'm doing wrong it would be much
apreciated. Thank you.
If you truly don't know how many instances you'll need, you probably just
want to use a std::vector to hold CSomeClass objects by value. If the
objects are expensive to copy, you can consider a vector of pointers
instead of objects; it makes the code a bit more complicated, but you
still get the benefit of automatic vector resizing as you add more
elements. And if you ever need to, say, sort the collection, that ends up
being a lot more efficient when you have an array of pointers than when you
have an array of objects.

Many folks these days would counsel you to just use a vector in any case.
In your case, I'd tend to agree with that. Old-style "dynamic arrays" are
becoming more of a special-case than the general rule. They have their
place when performance considerations force the issue, but vectors (and
other STL containers) are much more pleasant to code with, and lead to more
robust programs.
-leor

James Alger


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2

"James" <ja***********@hotmail.com> wrote in message
news:b7**************************@posting.google.c om...
Hi, I'm hoping someone can help me out.

If I declare a class, eg.

class CSomeclass
{
public:
var/func etc.....
private
varfunc etc..
};

Then in the code I create a pointer to this class definition

CSomeClass *MyClass;

I want to be able to dynamically increase the size of the
class as I go. Imagine I have a current count of the
object - eg. Count. Whenever I try to allocate
memory for it,

Count++;
MyClass[Count] = new SomeClass;


MyClass is a pointer to CSomeClass, so MyClass[Count] is a CSomeClass, but
new CSomeClass is a pointer. So you are assigning a CSomeClass* to a
CSomeClass, not surprisingly the compiler complains.

I think you don't have a good understanding of pointers and arrays, so I'll
recommend the easy way (I'd actually recommend this in any case). Here's a
very brief tutorial in vectors

#include <vector>

std::vector<CSomeClass> MyClass; // a zero sized vector
cout << MyClass.size() << '\n'; // this prints zero
MyClass.push_back(CSomeClass()); // add an object to the vector
cout << MyClass.size() << '\n'; // now this prints one
MyClass.push_back(CSomeClass()); // add another object to the vector
cout << MyClass.size() << '\n'; // now this prints two
for (int i = 0; i < MyClass.size(); ++i) // loop though all the objects in
the vector
{
CSomeClass obj = MyClass[i]; // get an object out the vector
// do something with it
}

Get the picture? Learn about vectors, they will make your life so much
easier.

If you insist on doing it with pointers, then realise that the only way to
do it is to allocate a whole new array, bigger than the old array and the
copy all the elements one by one from the old array to the new array and
then delete the old array.

john
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10666
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
2
10841
by: Phil... | last post by:
I am trying to figure out how to create an array that contains objects and not references to objects. I know how to do the latter, but not the former. Any help is appreciated. I have the...
2
4868
by: | last post by:
I have this class ------------- class Component { /*class*/ Data *d; /*class*/ Draw *a; }; ------------- from "Component" derive classes like "TextBox", "Button", "Label", "ComboBox" etc from...
2
6649
by: Poewood | last post by:
I have created control array classes for buttons and textboxes and affixed them to a form class. In VB I can easily create a module with fuctions that can be accessed from any class. How can I do...
15
1725
by: Yogi_Bear_79 | last post by:
Visual Studio .NET started complaing when the array was around 4000. I found that if I pasted the array in via notepad then opened Visual Studio it would work. Now my array is over 26,000 and...
1
1120
by: Poewood | last post by:
I have created control array classes for buttons and textboxes and affixed them to a form class. In VB I can easily create a module with fuctions that can be accessed from any class. How can I do...
9
2009
by: me | last post by:
Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am...
7
6410
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
272
13874
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
3
2625
by: Gacrux | last post by:
1) In a class named AnyIntegers, prompt the user to enter 100 integers into a two-dimensional array. Use a method countEvenNum of the class CollectEvenNumber to determine the total even number...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.