Question: I am trying to fill out a program where the method definitions will be written underneath the .h file. In that .h file, the attributes
I am trying to fill out a program where the method definitions will be written underneath the .h file.
In that .h file, the attributes are.

When I try to submit the program I get these logical errors
a)
Test at() method - out of bounds (uses parameterized constructor and size)
ERROR: Subscript out of range. std::out_of_range exception was not thrown
How do I get the program to output this when the vector is out of range?:
std::cerr
b)
Test erase() methods (uses default constructor, push_back, size, and [])
actual result: myvector contains: 5 7 8 9 10 expected result: myvector contains: 4 5 7 8 9 10
How do I get the program to display these results when testing the erase() methods? :
4 5 7 8 9 10
.
the program
--main cpp--
int main() { /* Type your test code here. You will not be submitting this code. It's only for testing. */ return 0; }
--vector .h---
// vector class template
#ifndef Vector_Vector_h #define Vector_Vector_h
#include
template
// increase capacity void increaseCapacity(unsigned long);
public: // Default constructor vector();
// Constructor declaration vector(unsigned long);
// Resize the vector - changes the size attribute void resize(unsigned long n);
// Resizes the vector and initializes the unused locations - updates the size attribute void resize(unsigned long n, const T &val);
// Copy constructor declaration vector(const vector &);
// Destructor declaration ~vector();
// Accessor to return the array size unsigned long size() const;
// Accessor to return the array capacity unsigned long capacity() const;
// Accessor to test empty status bool empty() const;
// Accessor to return a specific element T &at(unsigned long position);
// Overloaded [] operator declaration T &operator[](const unsigned long &);
// back element of the vector T &back();
// front element of the vector T &front();
void push_back(T); // New push_back member T pop_back(); // New pop_back member
// insert element at position int insert(unsigned long, const T &);
// erase a range of values void erase(unsigned long, unsigned long);
// erase one element at a position void erase(unsigned long); };
/* This is where your method definitions will be written. This default constructor * the first method you need to complete. To compile the program, you should create * empty stubs for each of the declared methods. */
template
} #endif
my code
--vector .h---
#define Vector_Vector_h
#include
template
// increase capacity void increaseCapacity(unsigned long);
public: // Default constructor vector();
// Constructor declaration vector(unsigned long);
// Resize the vector - changes the size attribute void resize(unsigned long n);
// Resizes the vector and initializes the unused locations - updates the size attribute void resize(unsigned long n, const T &val);
// Copy constructor declaration vector(const vector &);
// Destructor declaration ~vector();
// Accessor to return the array size unsigned long size() const;
// Accessor to return the array capacity unsigned long capacity() const;
// Accessor to test empty status bool empty() const;
// Accessor to return a specific element T &at(unsigned long position);
// Overloaded [] operator declaration T &operator[](const int &);
// back element of the vector T &back();
// front element of the vector T &front();
void push_back(T); // New push_back member T pop_back(); // New pop_back member
// insert element at position int insert(unsigned long, const T &);
// erase a range of values void erase(unsigned long, unsigned long);
// erase one element at a position void erase(unsigned long); };
/* This is where your method definitions will be written. This default constructor * the first method you need to complete. To compile the program, you should create * empty stubs for each of the declared methods. */
template
template
}
template
arraySize = n; }
template
for (unsigned long i = arraySize; i
arraySize = n; } catch(std::bad_alloc & exception){ std::cerr
template template template template } template } template template template } template template aptr[arraySize] = val; arraySize++; } template template if (position arraySize) { std::cout if (arraySize == arrayCapacity) { increaseCapacity(arrayCapacity * 2); } for (unsigned long i = arraySize; i > position; i--) { aptr[i] = aptr[i-1]; } aptr[position] = val; arraySize++; return 0; } template if (start >= end || start >= arraySize) { return; } end = (end >= arraySize) ? arraySize - 1 : end; const unsigned long rangeSize = end - start + 1; for (unsigned long i = end + 1; i arraySize -= rangeSize; } template if (position = arraySize) { std::cout for (unsigned long i = position; i arraySize--; } template T *tempArray = new T[newCapacity]; for (unsigned long i = 0; i delete [] aptr; aptr = tempArray; arrayCapacity = newCapacity; } catch (std::bad_alloc & exception) { std::cerr }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
