PDA

View Full Version : c++ program errors




bravens52
Apr 13, 2011, 10:03 PM
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

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



lee1210
Apr 13, 2011, 11:18 PM
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.

bravens52
Apr 13, 2011, 11:40 PM
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

SidBala
Apr 14, 2011, 12:23 AM
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.

SidBala
Apr 14, 2011, 03:17 AM
Use

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:
if (stacksize = stackcapacity)

Assignment vs equivalence. It should be:

if (stacksize == stackcapacity)


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.