Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am filling out a program where the method definitions will be written underneath the .h file in the .h file the attributes are When

I am filling out a program where the method definitions will be written underneath the .h file

in the .h file the attributes are

image text in transcribed

When I run the program I get this error

image text in transcribed

When I change void vector::push_back(T val)

I get this error message Is there anyway to fix these issues?

image text in transcribed

the program

--main cpp--

vector.h

#include #include "vector.h"

using namespace std;

int main() { /* Type your test code here. You will not be submitting this code. It's only for testing. */ return 0; }

// vector class template

#ifndef Vector_Vector_h #define Vector_Vector_h

#include #include // Needed for bad_alloc exception #include // Needed for the exit function #include namespace CIST2362 {

template class vector { private: T *aptr; // To point to the allocated array unsigned long arraySize; // Number of elements in the array unsigned long arrayCapacity; // number of memory locations available to the vector

// 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 vector::vector() { }

} #endif

my code

vector.h

// vector class template

#ifndef Vector_Vector_h #define Vector_Vector_h

#include #include // Needed for bad_alloc exception #include // Needed for the exit function #include namespace CIST2362 {

template class vector { private: T *aptr; // To point to the allocated array unsigned long arraySize; // Number of elements in the array unsigned long arrayCapacity; // number of memory locations available to the vector

// 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(int 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 vector::vector() { arraySize = 0; arrayCapacity = 10; aptr = new T[arrayCapacity]; }

template vector::vector(unsigned long size){ arraySize = size; arrayCapacity = size; aptr = new T[arrayCapacity];

}

template void vector::resize(unsigned long n){ if (n > arrayCapacity) { increaseCapacity(n); }

arraySize = n; }

template void vector::resize(unsigned long n, const T &val){ try{ if (n > arrayCapacity) { increaseCapacity(n); }

for (unsigned long i = arraySize; i

arraySize = n; } catch(std::bad_alloc & exception){ std::cerr

template vector::vector(const vector &obj){ try{ arraySize = obj.arraySize; arrayCapacity = obj.arrayCapacity; aptr = new T[arrayCapacity]; for (long unsigned i = 0; i

template vector::~vector(){ delete [] aptr; }

template unsigned long vector::size() const { return arraySize; }

template unsigned long vector::capacity() const { return arrayCapacity;

}

template bool vector::empty() const { return (arraySize == 0);

}

template T& CIST2362::vector::at(int position){ try{ if(position =arraySize) { std::cout

template T& CIST2362::vector::operator[](const int&index) { return aptr[index]; }

template T &vector::back() { return aptr[arraySize - 1];

}

template T &vector::front() { return aptr[0]; }

template void vector::push_back(T val) { int = T if (arraySize == arrayCapacity) { increaseCapacity(arrayCapacity * 2); }

aptr[arraySize] = val; arraySize++; }

template T vector::pop_back(){ return aptr[--arraySize]; }

template int vector::insert(unsigned long position, const T &val){

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 void vector::erase(unsigned long start, unsigned long end){

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 void vector::erase(unsigned long position){

if (position = arraySize) { std::cout

for (unsigned long i = position; i

arraySize--; }

template void vector::increaseCapacity(unsigned long newCapacity) { try{ if (newCapacity

T *tempArray = new T[newCapacity];

for (unsigned long i = 0; i

delete [] aptr;

aptr = tempArray;

arrayCapacity = newCapacity; } catch (std::bad_alloc & exception) { std::cerr

}

}

#endif

- T \&backi): access the last element in the vector You may change this name if you want:- - T \&front (): access the first element in the vector - unsigned long arraysize: The value that represents the current number of elements in Modifying methods: - unsigned long arraycapacity: The value that represents the current allocated memory - void push_back [T) : adds an element to the end of the vector capacity of the vector (must always be s=arraysize) - Tpop_back(1: removes the last element of the vector and returns it You will find the following private method: - int ingert(unsigned long, const T \&.): Inserts the element in the 2nd parameter at the - void increaseCapacity(unsigned long): This method is used for increasing the capacity of position given by the 1st parameter the vector. It should not be public, but other methods in the class would need it. throws - void erase(unsigned long, unsigned long): Removes the elements starting with the the std::bad_alloc() exception if memory cannot be allocated. NOTE: You may add more private methods if you see opportunities for code reuse across the needs of the public element at the position of the first parameter up to the element before the position in vector mathods. the 2 nd parameter i.e. do not remove the element at the position of the 2nd parameter. You will find the following public methods: There are 3 constructors. - void erase(unsigned long): Removes the element at the given position - vector]: This constructor creates a vector with a capacity of 10 and size of 0 . - vector(unsigned long): This constructor creates a vector with the capacity given by the parameter and size will be 0 . - vector(const vector \&.): This is the copy constructor for the class All constructors throw the std::bad_alloc() exception if memory cannot be allocated. There are 2 resize methods: - void resize(unsigned longj) Resize the vector based on this parameter. Resize can make it smaller or larger; if it's made smaller the capacity is unchanged (makes the vector more efficient). - void resize(unsigned long, const T \&.): Resize the vector, and if it's made larger then fill in the new elements with value of the Znd parameter. The resize methods throw the std::bad_alloc() exception if memory cannot be allocated The destructor: vector(): This needs to clean up all memory allocated to the vector Accessor methods: - unsigned long size() const: returns the size attribute of the vector - unsigned long capacity() const: returns the capacity attribute of the vector - bool empty] const: returns true if the vector is empty, and false otherwise - T \&at(unsigned long): access the element at the given index and throws the std:out_of_range exception if out of bounds - T \&operator[[const int \&]: access the element at the given index and does not check In file included from main.cpp:3: vector.h: In member function 'T\& CIST2362:ivector : :at(int) [with T= int]': vector.h:172:1: warning: control reaches end of non-void function [-Wreturn-type] 172} In file included from main.cpp:2: vector.h: In member function 'void CIST2362::vector T>: :push_back(T)': vector.h:192:7: error: expected unqualified-id before ' = ' token 192 | T= int

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions