Implementation of Vector in C++

Hello,

I am trying to write a program for implementing a vector data type using the following operations:

1.A default constructor, which should create a buffer with capacity 5 and size 0.
2.A constructor that takes an integer argument, and creates a buffer with the given size and capacity.
3.A member function set_capacity(int), which changes the capacity to the indicated limit, copying elements from the current buffer . If the new capacity if larger than the current size, the size remains unchanged; otherwise the size is made the same as the new capacity.
4.The operation at, which returns the element at the given location in the buffer.
5.The operation push_back, which adds the element to the end of the buffer, increasing the size, and invoking set_capacity to increase the capacity of the buffer if necessary


It throws bunch of errors which I have attached a screenshot. please assist. thank you
// VectorImplementation.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream> 
#include <string>
#include <cassert>
#include <algorithm> 
using namespace std;  

template <class T>
class Vector 
{ 
public:  
	typedef T * iterator;
	Vector();  
	Vector(unsigned int size); 
	Vector(unsigned int size, const T & initial); 
	Vector(const Vector<T> & v);    
	
	~Vector(); 
	unsigned int capacity() const;   
	unsigned int size() const;  
	bool empty() const; 
	iterator begin(); 
	iterator end(); 
	T & front();  
	T & back(); 
	
	void push_back(const T & value);    
	void pop_back();
	void reserve(unsigned int capacity);  
	void resize(unsigned int size);  
	T & operator[](unsigned int index);  
	
	Vector<T> & operator=(const Vector<T> &); 

private:  
	unsigned int my_size;  
	unsigned int my_capacity;  
	T * buffer;
};  
	
	
	template<class T>
	Vector<T>::Vector()
	{     
		my_capacity = 5; 
		my_size = 0;    
		buffer = 0;
	} 
	template<class T>
	Vector<T>::Vector(const Vector<T> & v)
	{    
		my_size = v.my_size;
		my_capacity = v.my_capacity;
		buffer = new T[my_size];   
		for (int i = 0; i < my_size; i++)     
			buffer[i] = v.buffer[i]; 
	} 
	template<class T> 
	Vector<T>::Vector(unsigned int size)
	{     
		my_capacity = size;   
		my_size = size;    
		buffer = new T[size];
	} 

	template<class T> 
	Vector<T>::Vector(unsigned int size, const T & initial) 
	{    
		my_size-size;    
		my_capacity = size; 
		buffer = new T [size];  
		for (int i = 0; i < size; i++) 
			buffer[i] = initial;    
		T();
	} 
	template<class T>
	Vector<T> & Vector<T>::operator = (const Vector<T> & v)
	{    
		delete[ ] buffer;
		my_size = v.my_size;  
		my_capacity = v.my_capacity;
		buffer = new T [my_size];  
		for (int i = 0; i < my_size; i++) 
			buffer[i] = v.buffer[i]; 
		return *this;
	} 

	template<class T> 
	typename Vector<T>::iterator Vector<T>::begin()
	{   
		return buffer;
	} 

	template<class T> 
	typename Vector<T>::iterator Vector<T>::end()
	{   
		return buffer + size();
	} 
	template<class T> 
	T& Vector<T>::Vector<T>::front()
	{    
		return buffer[0];
	}  
	
	template<class T>
	T& Vector<T>::Vector<T>::back() 
	{   
		return buffer[size - 1]; 
	}  

	template<class T>
	void Vector<T>::push_back(const T & v)
	{   
		if (my_size >= my_capacity) 
			reserve(my_capacity +5);
		buffer [my_size++] = v; 
	} 
	template<class T> 
	void Vector<T>::pop_back()
	{    
		my_size--;
	}  
	template<class T> 
	void Vector<T>::reserve(unsigned int capacity)
	{   
		if(buffer == 0)
		{     
			my_size = 0;    
			my_capacity = 0; 
		}      
		T * buffer = new T [capacity];  
		assert(buffer); 
		copy (buffer, buffer + my_size, buffer); 
		my_capacity = capacity; 
		delete[] buffer;  
		buffer = buffer; 
	} 
	template<class T> 
	unsigned int Vector<T>::size()const
		 {  
			 return my_size; 
	} 
	template<class T>
	void Vector<T>::resize(unsigned int size)
	{     
		reserve(size); 
		size = size; 
	}  
	template<class T>
	T& Vector<T>::operator[](unsigned int index)
	{  
		return buffer[index]; 
	}    
	template<class T> 
	unsigned int Vector<T>::capacity()const
	{   
		return my_capacity;
	}  
	template<class T>
	Vector<T>::~Vector()
	{    
		delete[]buffer; 
	}  
	int main()
	{    
		Vector<int> v; 
		v.reserve(2);
		assert(v.capacity() == 2); 
		Vector<string> v1(2);   
		assert(v1.capacity() == 2);  
		assert(v1.size() == 2); 
		assert(v1[0] == "");  
		assert(v1[1] == "");  
		v1[0] = "hi";
		assert(v1[0] == "hi");   
		Vector<int> v2(2, 7);  
		assert(v2[1] == 7);    
		Vector<int> v10(v2);  
		assert(v10[1] == 7); 
		Vector<string> v3(2, "hello"); 
		assert(v3.size() == 2); 
		assert(v3.capacity() == 2);  
		assert(v3[0] == "hello");  
		assert(v3[1] == "hello");  
		v3.resize(1);  
		assert(v3.size() == 1);
		assert(v3[0] == "hello");  
		Vector<string> v4 = v3;
		assert(v4.size() == 1); 
		assert(v4[0] == v3[0]); 
		v3[0] = "test"; 
		assert(v4[0] != v3[0]); 
		assert(v4[0] == "hello");  
		v3.pop_back(); 
		assert(v3.size() == 0);  
		Vector<int> v5(7, 9);  
		Vector<int>::iterator it = v5.begin();  
		while (it != v5.end())   
		{       assert(*it == 9); 
		++it;   
		}   
		Vector<int> v6; 
		v6.push_back(100); 
		assert(v6.size() == 1); 
		assert(v6[0] == 100);   
		v6.push_back(101);   
		assert(v6.size() == 2);
		assert(v6[0] == 100);   
		v6.push_back(101); 
		cout << "SUCCESS\n";
	}

Open in new window

Doc2.docx
angel7170Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi angel7170,

I'm not sure but I guess the problem is that in T& Vector<T>::Vector<T>::front() there's one Vector<T>:: too much - try T& Vector<T>::front() instead - the same is with back().

Hope that helps,

ZOPPO
0
sarabandeCommented:
if the vector should take integers only you better should not use a template class.

then you could replace T by int and implement the class according to your assignment which would have much less complexity than that what you posted.

Sara
0
AndyAinscowFreelance programmer / ConsultantCommented:
	void Vector<T>::reserve(unsigned int capacity)
	{   
		if(buffer == 0)
		{     
			my_size = 0;    
			my_capacity = 0; 
		}      
		T * buffer = new T [capacity];  
		assert(buffer); 
		copy (buffer, buffer + my_size, buffer); 
		my_capacity = capacity; 
		delete[] buffer;  
		buffer = buffer; 
	} 

Open in new window


That isn't correct - you have a local variable called buffer and a class member called buffer
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

sarabandeCommented:
sorry, forget my comment, i didn't read the requirements correctly.

Sara
0
angel7170Author Commented:
I still couldn't get this right.
0
angel7170Author Commented:
I've requested that this question be deleted for the following reason:

Didn't get my question answered.
0
ZoppoCommented:
Hi angel7170,

did you try what I suggested above? I still think the errors shown in the attached document are caused by double appearance of Vector<T>:: in implementation of the front (and back too allthough no errors are thrown because compilation stops before this function is reached).

IMO you just need to replace this
template<class T> 
T& Vector<T>::Vector<T>::front()
{    
	return buffer[0];
}  

template<class T>
T& Vector<T>::Vector<T>::back() 
{
	return buffer[size - 1]; 
}

Open in new window

with this
template<class T> 
T& Vector<T>::front()
{    
	return buffer[0];
}  

template<class T>
T& Vector<T>::back() 
{   
	return buffer[size - 1]; 
}

Open in new window

Did you try this?

ZOPPO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
I got the source compiled by doing the following changes:

line 104:
T& Vector<T>::Vector<T>::front()

Open in new window

to

T& Vector<T>::front()

Open in new window

as already told by Zoppo.

line 110:
T& Vector<T>::Vector<T>::back()

Open in new window

to

T& Vector<T>::back()

Open in new window

as already told by Zoppo.

line 59:
for (int i = 0; i < my_size; i++)

Open in new window

to
for (int i = 0; i < (int)my_size; i++)

Open in new window

that was a warning only because I was signed integer while my_size was unsigned.

Sara
0
ZoppoCommented:
I now cancel this deletion request since I'm quite sure the reason I mentioned in my first post is the cause for the errors and the solution I presented should work. This is even aproved by sarabande's last comment.

@angel7170: You wrote I still couldn't get this right. Could you explain what you tried and/or post the code with the suggested modifications done? Do you get other error messages or still the same?

ZOPPO
0
angel7170Author Commented:
I've requested that this question be deleted for the following reason:

Don't need the answer.
0
sarabandeCommented:
Zoppo has answered the question with http:#a27479468 .

the code compiles when following the advise given.

the question and solution handle typical use case for beginner's programming issue and should be added to the knowledge base.

Sara
0
ZoppoCommented:
I agree ...
0
sarabandeCommented:
sorry Zoppo, I didn't want these points ...

Sara
0
ZoppoCommented:
No problem, Sara, I don't care much about the points. And you mentioned the warning I forgot about ...

Best regards,

ZOPPO
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.