Archived

This topic is now archived and is closed to further replies.

C++ Class with pointer array.. HELP please

This topic is 5172 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi fellas (and ladies), i'm trying to make a CList class which basically has an array inside it that stores pointers to anything(classes/structs/ints etc). This is so in my game i can basically have a 'set' of models, moving models etc... i don't want to write specific lists for each set, as this will get just too stupid & out of control before long. (as i get more and more sets) Remember, i'm trying to store only the pointer to the object, not the actual object itself in the array in the class. Below is my code: class CPointerList { void* list; int numElements; int arraySize; CPointerList() { numElements=0; arraySize=DEFAULT_LIST_SIZE; list = new void*[arraySize]; } CPointerList(int size) { numElements=0; arraySize=size; list = new void*[arraySize]; } ~CPointerList() { delete[] list; } void addElement(void* elem) { if (numElements==arraySize) { //out of space //need to write grow array code here MessageBox(NULL, "FINISH ME!!", NULL, NULL); } &list[numElements++]=&elem; //add elem } }; visual C++ (v6) is complaining that: 'void *' : unknown size, and '=' : left operand must be l-value both at the "&list[numElements++]=&elem;" line i think it's trying to copy the whole 'object' at elem into the array, however i just want memory address of it! can someone please point me in the right direction? i've tried searching for a solution, but haven't found anything.. many thanks in advance to anyone who can shed some light. oh did i mension thanks? -RaiLaN. PS, i haven't even got to the get() methods yet. thanks again! [edited by - RaiLaN on October 15, 2003 11:49:32 AM]

Share this post


Link to post
Share on other sites
hm.. maybe a simple list would be easier, or use a base class to get rid of those void pointers. btw. what you are doing is a dynamic array, not a list ,-)

anyway.. list is a pointer to void pointers, so its void** list and not void* list.

also this line is really horrible if you think about it and kind of suggests that you havent coded it out of understanding but to somehow make your compiler happy:

&list[numElements++]=&elem;

list is a void*, so list[x] is a "void" and &list[elem] is the address of this void, ie the address of a position in your array and you really do NOT want to change that (also: you cant.. you can write down your address, but you cant change your address to be somewhere else). elem is a copy of a void*, but your array is storing voids (list is declard as void* instead of void**), taking the address of this pointer is making it even worse (trying to store a void** where a void would belong).. also, taking the address of a local variable is bad, because this variable (a copy of the original value) will not exist anymore once you leave the function.

in other words your local &elem is something completely different from the &element at wherever you call addElement(element).


after changing list to void** change this line to:
list[numElements++]=elem;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi.

Unless you are programming your CList for the learning experience, I suggest that you use one of the containers in the STL. After all, that is what they are there for. If you don''t use the STL, you are just wasting time.

So, if you are doing this for experience then you must know about what you can and can not do with pointers to void.

Much of your code does make any sense at all, so you should re-think what you are doing.

"void" is syntactically a fundamental type, but you can not use it in every way in which regular types can be. You can not make a plain void, it can only be used in in a more complicated type such as void*. The only safe operations on a void* are assignment to another void*, !=, ==, conversion to another type, assignment of a type to a void*.

So, knowing that, take a look at your allocation line for one, and you''ll see that it doesn''t make any sense.

Hope that helps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
One last thing,

You are wondering what your array indexing is not working? Think about how array indexing works. In order to point to the next array element the compiler has to know the size of the array objects, and it only knows that for fundamental and user-defined types, but you are trying to get it to work with the size of voids.. and what is their size? (:

Share this post


Link to post
Share on other sites