-6

I'm trying to preform a Deep Copy of one class to another. Using VS2015.

below on *(clsOriginalToCopy + lngIndex); is where I get the error, I am at a loss.

for (lngIndex = 0; lngIndex < lngSize; lngIndex += 1)
{
    *(this + lngIndex) = *(clsOriginalToCopy + lngIndex);
}

edit

Error C2784 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(std::basic_string<_Elem,_Traits,_Alloc> &&,const _Elem)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &&' from 'const CResizableArray' Homework 8 - DeepCopy 479

edit

MCVE. It wants me to add more details so enjoy this excerpt from

"Hitchhikers guide to the galaxy"

“For instance, on the planet Earth, man had always assumed that he was more intelligent than dolphins because he had achieved so much—the wheel, New York, wars and so on—whilst all the dolphins had ever done was muck about in the water having a good time. But conversely, the dolphins had always believed that they were far more intelligent than man—for precisely the same reasons.” ― Douglas Adams, The Hitchhiker's Guide to the Galaxy

CResizableArray::CResizableArray()
{
    Initialize(0, 0);
}

CResizableArray::CResizableArray(long lngSize)
{
    Initialize(lngSize, 0);
}

CResizableArray::CResizableArray(long lngSize, long lngValue)
{
    Initialize(lngSize, lngValue);
}

void CResizableArray::Initialize(long lngSize, long lngValue)
{
    m_lngArraySize = 0;
    m_palngValues = 0;

    SetSize(lngSize, lngValue);
}

void CResizableArray::operator = (const CResizableArray &clsOriginalToCopy)
{
    if (this != &clsOriginalToCopy)
    {
        CleanUp();
        DeepCopy(clsOriginalToCopy);
    }
}

CResizableArray::~CResizableArray()
{
    CleanUp();
}


void CResizableArray::CleanUp()
{
    SetSize(0, 0);
}

void CResizableArray::SetSize(long lngNewSize)
{
    SetSize(lngNewSize, 0);
}

void CResizableArray::SetSize(long lngNewSize, long lngValue)
{
    long* palngNewValues = 0;
    long lngIndex = 0;
    long lngStop = 0;

    if (lngNewSize <      0) lngNewSize = 0;
    if (lngNewSize > 100000) lngNewSize = 100000;

    palngNewValues = new long[lngNewSize];

    for (lngIndex = 0; lngIndex < lngStop; lngIndex += 1)
    {
        *(palngNewValues + lngIndex) = lngValue;
    }

    if (lngNewSize < m_lngArraySize) lngStop = lngNewSize;
    else                             lngStop = m_lngArraySize;

    for (lngIndex = 0; lngIndex < lngStop; lngIndex += 1)
    {
        *(palngNewValues + lngIndex) = *(m_palngValues + lngIndex);
    }

    if (m_palngValues != 0)
    {
        delete[] m_palngValues;
        m_palngValues = 0;
    }

    m_palngValues = palngNewValues;

    m_lngArraySize = lngNewSize;
}

void CResizableArray::DeepCopy(const CResizableArray &clsOriginalToCopy)
{
    long lngSize = 0;
    long lngIndex = 0;

    lngSize = clsOriginalToCopy.GetSize();

    SetSize(lngSize);

    for (lngIndex = 0; lngIndex < lngSize; lngIndex += 1)
    {
        *(this + lngIndex) = *(clsOriginalToCopy + lngIndex);
    }
}

reply from instructor

The "this" key word is a pointer to the current active instance, which in this case is an instance of CResizableArray.

You have "this + lngIndex" which is trying to add an integer to it. That would make sense if you had an array of instances and you want to jump to an instance with an index further down the list. But you don't have an array of instances. You have an instances which happens to have an array inside of it.

So, you are trying to move up and down outside of the class. What you really want to do is move up and down inside of the class.

reply from instructor

Adam Schneider
  • 275
  • 1
  • 5
  • 18
  • 1
    Please post an [mcve](http://stackoverflow.com/help/mcve) – Mohamad Elghawi Feb 09 '16 at 14:14
  • 1
    please post an [mcve] – NathanOliver Feb 09 '16 at 14:14
  • 2
    What type is clsOriginalCopy? It should work if it's a pointer and lngIndex is an integer typ, but if clsOriginalCopy is an object it will not work. – Mats Fredriksson Feb 09 '16 at 14:14
  • Please, please, please: encoding the type of a variable in its name was discredited hundreds of years ago. Don't do that `palng` stuff. – Pete Becker Feb 09 '16 at 14:40
  • Read about `std::copy`. You'll save yourself many headaches. Even better, unless this is an exercise, use `std::vector`. – Pete Becker Feb 09 '16 at 14:42
  • this is an exercise. My instructor on purpose does things the hard way. But i have been googling for hours, Emailing him, and, looking through the text book "he never uses" for a hint as to why this is happening. I don't even actually need a solution just an explanation as to why this is happening would probably point ... *smirks* me in the right direction. – Adam Schneider Feb 09 '16 at 14:58
  • Would you mind to tell us what exactly is your error? Compiler error rundime error? Please add the error text verbatim into your question. – πάντα ῥεῖ Feb 09 '16 at 15:04
  • @πάνταῥεῖ I updated the question with the compiler error. – Adam Schneider Feb 09 '16 at 15:57

1 Answers1

0

First of all, in code you have provided I do not see operator +, so compiler is absolutely right.

The second point I do not understand is *(clsOriginalToCopy + lngIndex) expression... What your operator + should return? Pointer? Why not reference (considering your operator =)?

Try to redesign your class, perhaps operator + is not really needed and only some changes in DeepCopy implementation will resolve the issue

VolAnd
  • 6,367
  • 3
  • 25
  • 43