Can someone help me finish writing this code? Ive made it so that my_vector.cpp compiles. However, i need to complete my_vector.cpp using my_vector.h file.
#ifndef MY_VECTOR_H
#define MY_VECTOR_H
// my_vector.h
usingnamespace std;
template <class T>
class my_vector
{
public:
// Constructors / Destructor
my_vector();
// Constructor with initial size
my_vector(unsignedint size);
// Constructor with initial size -- set all initial spaces to initial param
my_vector(unsignedint size, const T & initial);
// Copy constructor
my_vector(const my_vector & v);
~my_vector();
// Return capacity of vector (in elements)
int capacity() const;
// Return the number of elements in the vector
unsignedint size() const;
// Return true if vector has no elements
bool empty() const;
// Add a new element to the end of the backing array
// Don't forget to resize if needed!
void push_back(const T & value);
// Remove the last element (don't return it)
void pop_back();
// Adjust capacity (make more space as needed)
void reserve(unsignedint new_capacity);
// Resize vector (even if that means making it smaller)
void resize(unsignedint new_size); // adjust size
/* Overloaded operators */
// Return a *reference* to numbered element
T & operator [ ](unsignedint index) const;
private:
unsignedint my_size;
unsignedint my_capacity;
T * buffer;
};
/* put all your code in my_vector.cpp */
#include "my_vector.cpp"
#endif
Wait a minute... I recall there being something with template definitions in a C++ file generating errors... and considering how templates are implemented... ah, sleepy day. *shakes self*
As for your actual code, have you covered dynamic memory allocation? As in new and delete? Those would help you a lot with push_back(), pop_back(), reserve(), and resize().
well i got help with that piece of work but i know that a new array was made and put into buffer. I believe now we place all the initial values into this array but i dont know how to do that. thank you for helping even though your half asleep :D
The constructor and destructor functions are giving me trouble. i went to get help but they werent much help. Oh i already read that and but i dont know how to apply it.
And im not sure about my_capacity[i]. I believe it puts the items i have in capacity to buffer but im not sure if im right.
Firstly, my_capacity is an unsigned integer, so [i] might cause problems, as might the fact that buffer is a template... (oh eff).
Constructor 1:
Set everything to NULL.
Constructor 2:
1 2 3
my_size = size;
my_capacity = my_size;
buffer = new T[my_size]; //This is a solution. I really shouldn't have given it....
Constructor 3:
1 2 3
my_size = size;
my_capacity = my_size;
buffer = new T[my_size];
You will then need a for loop to copy initial into each space of your reserved space. I hope you can figure that out.
Copy Constructor:
I leave that one to you. Consider: my_size will equal my_capacity, which will equal the size of the other vector, and not only can you reserve your space ahead of time based on that, but you can also use it for the bounds of your loop, which will copy the elements from the other vector to the initialized vector one by one.
Could you be more specific? What does it do that you don't what it to do? I'm not really interested in trying to figure out what the problem is in addition to figuring out how to solve it.