Home > Back-end >  Vec3 error: 0 xc0000005: when writing position 0 XDDDDDDDD access conflict. For bosses to solve? In
Vec3 error: 0 xc0000005: when writing position 0 XDDDDDDDD access conflict. For bosses to solve? In

Time:09-28

# # ifndef MYVECTOR_H
# define MYVECTOR_H

# include & lt; Iostream>
# include & lt; Cstring>

Using namespace STD.

//custom container class
Template
The class MyVector
{
Public:
MyVector ();
MyVector (int len, T element);//fill len length of element element
MyVector (const MyVector & Vec);//copy construction
Virtual ~ MyVector ();

TemplateFriend ostream& Operator<(ostream& Out, const MyVector
MyVector & Operator=(const MyVector & Vec);

Protected:

Private:
T * m_elements;//used to store elements of the array
Int m_length;//all the storage elements, the actual number of
Int m_capacity;//the size of the current element array
};

Template
MyVector : : MyVector () : m_capacity (16), m_length (0)
{
This - & gt; M_elements=new T [m_capacity];
}

Template
MyVector : : MyVector (int len, T element) : m_capacity (16)
{
M_capacity=len + m_capacity;
M_length=len;
M_elements=new T [m_capacity];

for (int i=0; I & lt; M_length; I++)
{
Memcpy (& amp; M_elements [I], & amp; Element, sizeof (T));
}
}

Template
MyVector & MyVector : : operator=(const MyVector & Vec)
{
If (this==& amp; Vec)
Return * this;
If (this - & gt; M_elements!=nullptr)
{
The delete [] this - & gt; M_elements;
This - & gt; M_elements=nullptr;
This - & gt; M_capacity=16;
This - & gt; M_length=0;
}
This - & gt; M_capacity=vec. M_capacity;
This - & gt; M_length=vec. M_length;
This - & gt; M_elements=new T [vec m_capacity];
Memcpy (m_elements, vec. M_elements m_length * sizeof (T));
}

Template
MyVector : : MyVector (const MyVector & Vec)
{
This - & gt; M_capacity=vec. M_capacity;
This - & gt; M_length=vec. M_length;
This - & gt; M_elements=new T [vec m_capacity];
Memcpy (m_elements, vec. M_elements m_length * sizeof (T));
}

Template
T& MyVector : : operator [] (int index)
{
Return m_elements [index];
}

Template
Void MyVector Element: : push_back (T)
{
If (nullptr==m_elements)
{
M_capacity=16;
M_length=0;
M_elements=new T [m_capacity];
}
//determine whether the current array is full
If (m_length==m_capacity)
{
//if full, expansion: * 2 + 1, the current capacity expansion and can also be a fixed value, according to the actual demand
T * newElement=new T [m_capacity * 2 + 1);
//copy the elements of the original into the new space in
Memcpy (newElement m_elements, m_length * sizeof (T));
The delete [] m_elements;
M_elements=newElement;
}
Memcpy (& amp; M_elements [m_length + +], & amp; Element, sizeof (T));
}

TemplateOstream& Operator<(ostream& Out, const MyVector{
for (int i=0; I & lt; Vec. M_length; I++)
{
The out & lt; }
The out & lt; return out;
}

Template
MyVector : : ~ MyVector ()
{
The delete [] m_elements;
}

# endif//MYVECTOR_H


//the main CPP

# include & lt; Iostream>
#include
# include "MyVector. H"

Using namespace STD.

Void TestVector ();

Int main ()
{
TestVector ();

return 0;
}

Void TestVector ()
{
MyVector Vec1;
Cout & lt; <"Vec1:" & lt; MyVector Vec2 (10, 99.9);
Cout & lt; <"Vec2:" & lt; //MyVector Vec3 (8, string (" ABC "));
//cout & lt; <"Vec3:" & lt;
//vec3) push_back (" GHK ");
//cout & lt; <"After insert element \ n" & lt; <"Vec3:" & lt;
MyVector Vec4 (vec2);
Cout & lt; <"Vec4:" & lt; MyVector Vec5 (20, 66.6);
Cout & lt; <"Vec5:" & lt; Vec5=vec2;
Cout & lt; <"Equal assignment vec5=vec2 vec5:" & lt; }
  • Related