Question
Hi I have an error in my code. How can I fix it? Thanks: Header: #ifndef DynamicArray_h #define DynamicArray_h #include using namespace std; template class
Hi I have an error in my code. How can I fix it? Thanks:
Header:
#ifndef DynamicArray_h
#define DynamicArray_h
#include
using namespace std;
template
class DynamicArray
{
V* values;
int cap;
V dummy;
public:
DynamicArray(int = 2);
DynamicArray(const DynamicArray
~DynamicArray() { delete[] values; }
int capacity() const { return cap; }
void capacity(int);
V operator[](int) const;
V& operator[](int);
DynamicArray
};
template
DynamicArray
{
this->cap = cap;
values = new V[cap];
for (int index = 0; index
values[index] = V();
}
}
template
V DynamicArray
{
if (index = cap)
return V(); // a copy
return values[index]; // a copy
}
template
V& DynamicArray
{
if (index
return dummy; // a copy
}
else if (index >= cap) {
capacity(2 * index);
}
return values[index]; // a copy
}
template
void DynamicArray
V* temp = new V[newCap];
// get the lesser of the new and old capacities
int limit = min(newCap, cap);
// copy the contents
for (int i = 0; i
temp[i] = values[i];
}
// set added values to their defaults
for (int i = limit; i
temp[i] = V();
}
// deallocate original array
delete[] values;
// switch newly allocated array into the object
values = temp;
// update the capacity
cap = newCap;
}
template
DynamicArray
{
cap = original.cap; // still copy
values = new V[cap]; // not copy, is new
for (int i = 0; i
values[i] = original.values[i];
}
}
template
DynamicArray
{
if (this != &original) //check if copy or not, better not be tho
{
// same as destructor
delete[] values;
// same as copy constructor
cap = original.cap;
values = new V[cap]; // not copy, is new
for (int i = 0; i
values[i] = original.values[i];
}
}
return *this; // return self reference
}
#endif
Code:
#include
#include
using namespace std;
#include
#include "DynamicArray.h"
int main()
{
DynamicArray
cout
cout
for (int i = 0; i
assert(a[i] == 0);
}
cout
a[1] = 123;
a[2] = 546;
cout
cout
assert(123 == a[1]);
cout
cout
assert(546 == a[2]);
a[-1000] = 222;
cout
cout
assert(123 == a[1]);
assert(546 == a[2]);
assert(222 == a[-3]);
assert(0 == a[100]); // should increase array capacity here and make new values 0 (for int)
assert(222 != a[11]);
assert(222 != a[0]);
cout
const DynamicArray
for (int i = 0; i
assert(a[i] == b[i]);
}
cout
const DynamicArray
assert(c.capacity());
assert(c[0] == c[0]);
cout
DynamicArray
assert(a.capacity() == d.capacity());
for (int i = 0; i
assert(a[i] == d[i]); // uses the setter version for both a and b
}
// object assignment test
cout
DynamicArray
e = a;
assert(a.capacity() == e.capacity());
for (int i = 0; i
assert(a[i] == e[i]);
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//testing for char
cout
DynamicArray
cout
cout
for (int i = 0; i
assert(ch[i] = 'A');
}
cout
ch[1] = 'A';
ch[2] = 'B';
cout
cout
assert('B' == ch[2]);
ch[-1000] = 'Z';
cout
cout
assert('A' == ch[1]);
assert('B' == ch[2]);
cout
const DynamicArray
for (int i = 0; i
assert(ch[i] == ch2[i]);
}
cout
const DynamicArray
assert(ch3.capacity());
cout
DynamicArray
assert(ch.capacity() == ch4.capacity());
for (int i = 0; i
assert(ch[i] == ch4[i]); // uses the setter version for both a and b
}
// object assignment test
cout
DynamicArray
ch5 = ch;
assert(ch.capacity() == ch5.capacity());
for (int i = 0; i
assert(ch[i] == ch5[i]);
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//testing for float
cout
DynamicArray
cout
cout
for (int i = 0; i
{
assert(f1[i] = 10.5);
}
cout
f1[1] = 12.5;
f1[2] = 20.5;
cout
cout
assert(20.5 == f1[2]);
f1[-1000] = 100.5;
cout
cout
assert(12.5 == f1[1]);
assert(20.5 == f1[2]);
cout
const DynamicArray
for (int i = 0; i
assert(f1[i] == f2[i]);
}
cout
const DynamicArray
assert(f3.capacity());
cout
DynamicArray
assert(f1.capacity() == f4.capacity());
for (int i = 0; i
assert(f1[i] == f4[i]); // uses the setter version for both a and b
}
// object assignment test
cout
DynamicArray
f5 = f1;
assert(f1.capacity() == f5.capacity());
for (int i = 0; i
assert(f1[i] == f5[i]);
return 0;
}
Microsoft Visual C+Runtime Library Debug Error! Program: ..al studio 2015 Projects Assignment 05 DebugAssignment 05.exe abort) has been calle (Press Retry to debug the application) Abort Retry gnoreStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started