Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bravens52

macrumors regular
Original poster
Jul 16, 2009
110
0
Code:
Stack.cpp: In copy constructor ‘Stack::Stack(const Stack&)’:
Stack.cpp:48: error: cannot convert ‘int*’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’
Stack.cpp: In member function ‘Stack& Stack::operator=(const Stack&)’:
Stack.cpp:67: error: cannot convert ‘int*’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’
Stack.cpp: In member function ‘void Stack::push(int)’:
Stack.cpp:167: error: cannot convert ‘int*’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’
Stack.cpp:169: error: cannot convert ‘int**’ to ‘int*’ in assignment
Stack.cpp: In member function ‘void Stack::pop() const’:
Stack.cpp:189: error: decrement of data-member ‘Stack::stacksize’ in read-only structure
Code:
Stack::Stack(const Stack& s)
{
        stacksize = s.stacksize;
        stackcapacity = s.stackcapacity;
        stackarray = new int [stackcapacity];
        strcpy(stackarray, s.stackarray);
}

Stack& Stack::operator=(const Stack& rightOp)
{
        if(this == &rightOp)
                {
                        return *this;
                }
                delete[] stackarray;
                stacksize = rightOp.stacksize;
                stackcapacity = rightOp.stackcapacity;
                stackarray = new int [stackcapacity];
strcpy(stackarray, rightOp.stackarray);
return *this;
}

void Stack::push(int)
{
if (stacksize = stackcapacity)
{
stackcapacity = stacksize * 2;
int* p;
p = new int[stackcapacity];
strcpy(stackarray, p);
delete stackarray;
stackarray = &p;
}

}
i want to know how do i copy the values of the stack array into s.stackarray.
I think I can do it with a for loop but i dont know how..can anyone help
 
Last edited by a moderator:
You can use a for loop, assigning one element of time starting with index 0 through stacksize-1. You could also use memcpy, copying sizeof(int)*stacksize bytes.

-Lee

Also, &p is not the base of that array, p is.
Also, pop is going to modify the object, so it's not construction.
Changing these plus removing all strcpys and replacing them with memcpys should help.
 
You can use a for loop, assigning one element of time starting with index 0 through stacksize-1. You could also use memcpy, copying sizeof(int)*stacksize bytes.

-Lee

Also, &p is not the base of that array, p is.
Also, pop is going to modify the object, so it's not construction.
Changing these plus removing all strcpys and replacing them with memcpys should help.

could you possibly show me how to do it?? ive never learned memcpys..im guessing she wants us to use a for loop
 
There are a few errors I can identify. Why don't you repost the code with the
Code:
 tags?

Also, lets see what you came up with for the loop copy solution.
 
Use

Code:
memcpy (destination, source, num );

Where num is the size in bytes of the array you want to copy into. Get this by doing stackcapacity * sizeof(int).

also note:
Code:
if (stacksize = stackcapacity)

Assignment vs equivalence. It should be:
Code:
if (stacksize == stackcapacity)


Code:
stackarray = &p;

p is a pointer. So don't take the '&' of it. If you do, then you are wanting the address to the pointer which holds the address of the array. That you don't want.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.