c++ program errors

Discussion in 'Mac Programming' started by bravens52, Apr 13, 2011.

  1. bravens52, Apr 13, 2011
    Last edited by a moderator: Apr 14, 2011

    macrumors regular

    Joined:
    Jul 16, 2009
    #1
    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
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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.
     
  3. thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #3
    could you possibly show me how to do it?? ive never learned memcpys..im guessing she wants us to use a for loop
     
  4. macrumors 6502a

    Joined:
    Jun 27, 2010
    #4
    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.
     
  5. macrumors 6502a

    Joined:
    Jun 27, 2010
    #5
    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.
     

Share This Page