Simple Object Array Question (C++)

Started by
13 comments, last by the_edd 16 years, 1 month ago
So my university exclusively teaches Java (a class on ada/prolog, but that's it). So I decided I needed to learn C++ on my own, so I've been going back and doing old java projects in C++. The one I'm doing now is a simple resizeable stack implementation. It's the first of these projects I'm attempting, and I'm stuck on an array of objects. I decided to just jump into it and use Templates, pointers, the whole 9 yards. So here's my array declaration:

T* base;  //our starting array, can be resized
and then later on in the constructor:

base = new T[max_size];    //init our array
which I think is the correct way to make an array of pointers. However I'm having issues compiling this method:

    /*
        Pushes an object onto our stack
    */
    void push(T* object){

        //check our stacks size
        if(curr_index >= max_size)
            resize(max_size + 3);

        base[curr_index++] = object;
    }
I know it's a dumb thing, but I can't seem to figure out nor could I find an example that I could understand. Any help would be appreciated.
Advertisement
You are creating an array of T-objects. Your push method wants to assign a pointer-to-T to one of those objects.
This should fix it:
T** base;base = new T*[max_size];

What error are you getting? If you want an array of pointers you would need to say: T** base = new T*[max_size];
That did it, thanks a ton guys.

my understanding was that T* base[] would create an array of T*'s (pointer to T objects).

Thanks for the help.
A pointer to T is roughly equivalent to an array-of-T variable. Not to an array-of-pointers-to-T. So:
T array_of_T[15];T* pointer_to_T = array_of_T;//which is equivalent topointer_to_T = &(array_of_T[0]);
awesome thanks for the clarification.

I've got another issue, I figured I'd just use this thread since it's related.

My function to resize my array is being a bit...wonky. I found a great sample in an old post on these forums, but it seems to just be copying the last address to the entire new array. Here's some sample output when it resizes:

Here are the values
i[0] Copying: 6 to 6i[1] Copying: 6 to 6i[2] Copying: 6 to 6i[3] Copying: 6 to 6i[4] Copying: 6 to 6


And the addresses

i[0] Copying: 0x22ff48 to 0x22ff48i[1] Copying: 0x22ff48 to 0x22ff48i[2] Copying: 0x22ff48 to 0x22ff48i[3] Copying: 0x22ff48 to 0x22ff48i[4] Copying: 0x22ff48 to 0x22ff48



Here's the code:

/*        resizes the array to the desired size    */    void resize(int size){        int newSize;        //check for size issues        if(max_size > size)            return; //doign anything to an equal or smaller sized array would be pointless        else            newSize = size;        T** temp = new T*[newSize]; //make our array        for(int i = 0; i < max_size; i++) {            temp = base;            cout << "i[" << i << "] Copying: " << base << " to " << temp << endl;        }        max_size = newSize;    //reset our current max size        //gonna delete the old array, and point it to the new one        delete[] base;        base = temp;    }


The current call to resize is max_size+3 and the initial base array is a size of 5.

Any insight would eb appreciated
You're outputting the text after you copy the data.
NextWar: The Quest for Earth available now for Windows Phone 7.
Oh maybe I should clarify, the array currently holds numbers 1-5 with the number 6 being the newest addition to the array (however it's not yet added since the array is of size 5 and needs to be resized first)

so the correct output should be 1-5 regardless of where the print is. Unless I'm seeing it completely wrong.

Thanks though.
Quote:Original post by Sh3rlok
Oh maybe I should clarify, the array currently holds numbers 1-5


Are you sure about that?
/*
resizes the array to the desired size
*/
void resize(int size){

int newSize;

//check for size issues
if(max_size > size)
return; //doign anything to an equal or smaller sized array would be pointless
else
newSize = size;

T** temp = new T*[newSize]; //make our array

for(int i = 0; i < max_size; i++) {
temp = base;
cout << "i[" << i << "] Copying: " << base << " to " << temp << endl;
}

max_size = newSize; //reset our current max size

//gonna delete the old array, and point it to the new one
delete[] base;

base = temp;
}

This topic is closed to new replies.

Advertisement