Sep 18, 2010 at 9:17am UTC
I'm learning template and trying to built fixed_vector which will function same as STL vector but i'm facing strange problem and i'm not able to compile the code.
Here I'm trying to make copy constructor which can copy different size of fixed_vector types.
FYI!
I have also implemented copy constructor which can copy same type and size of objects.
IDE: MS visual studio 8
Project: Win32 console application
Here is entire code:
#include "stdafx.h"
#include <vector>
using namespace std;
template<class T,size_t size>
class fixed_vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
iterator begin();
iterator end();
const_iterator begin() const {return _v};
const_iterator end() const{return _v+size};
T& operator [] (size_t pos) { return _v[pos];}
fixed_vector<T,size>& operator = (fixed_vector<T,size>& x) {
copy(x.begin(),x.end(),begin());
return *this;};
template<class T1,size_t size1> fixed_vector<T1,size1>& operator = (fixed_vector<T1,size1>& x) {
copy(x.begin(),min(size,size1)+x.begin(),begin());
return *this;};
fixed_vector();
fixed_vector(fixed_vector<T,size>& x);
template<class T1,size_t size1> fixed_vector(fixed_vector<T1,size1>& x){
copy(x.begin(),min(size1,size)+x.begin(),begin());
return;}
private:
T _v[size];
};
template<class T,size_t size> fixed_vector<T,size>::fixed_vector(){}
template<class T,size_t size> fixed_vector<T,size>::fixed_vector(fixed_vector<T,size>& x)
{
copy(x.begin(),x.end(),begin());
}
template<class T,size_t size> T* fixed_vector<T,size>::begin()
{
return _v;
}
template<class T,size_t size> T* fixed_vector<T,size>::end()
{
return _v+size;
}
int _tmain(int argc, _TCHAR* argv[])
{
int number=0;
fixed_vector<int,10> mycontainer;
fixed_vector<int,10>::iterator _it;
mycontainer[0]=10;
fixed_vector<int,20> mycontainercopied(mycontainer);
fixed_vector<int,10> samemycontainer(mycontainer);
fixed_vector<int,20> samemycontainercopied(mycontainercopied);
samemycontainercopied=samemycontainer;
return 0;
}
Error:
Error 4 error C2440: 'return' : cannot convert from 'fixed_vector<T,size>' to 'fixed_vector<T,size> &' e:\sujal\personal\tutorial\ecpp_item4_maximallyreusablegenericcontainer\ecpp_item4_maximallyreusablegenericcontainer\ecpp_item4_maximallyreusablegenericcontainer.cpp 26
Please help me to resolve this issue.
Sep 18, 2010 at 9:55am UTC
Hi All,
I have found the solution of error.
So please now don't spend your valuable time.
Thanks.
Sep 18, 2010 at 12:47pm UTC
Hi guestgulkan,
Ya you are right. That was only the problem.
Thanks for your time and solution.