Hello,
I stuck in my fixed_vector implementation. Compiler's error message is
"C2228: left of '.begin' must have class/struct/Union. My template copy
constructor cause to this error message. I'm putting all my class' code
below and below the implementation code I put my client code. Any help
will be appreciated.
#ifndef _FIXEDVECTOR_HPP_
#define _FIXEDVECTOR_HPP_
#include <cstdio>
#include <algorithm>
template <typename T, size_t capacity>
class fixed_vector {
T m_vd[capacity];
int m_size;
public:
typedef T* iterator;
typedef const T* const_iterator;
fixed_vector()
{
m_size = 0;
for (int i = 0; i < capacity; ++i)
m_vd[i] = 0;
}
template <typename U, size_t ucapacity>
fixed_vector(const fixed_vector<U, ucapacity> &r)
{
std::copy(r.begin(), r.begin() + min(capacity, ucapacity),
this.begin()); // Error line !
}
template <typename U, size_t ucapacity>
fixed_vector<T, capacity> &operator=(const fixed_vector<U,
ucapacity> &r)
{
std::copy(r.begin(), r.begin() + std::min(capacity, ucapacity),
this.begin());
return *this;
}
iterator begin() {return m_vd;}
iterator end() {return m_vd + capacity;}
const iterator begin()const {return m_vd;}
const iterator end()const {return m_vd + capacity;}
int length() {return m_size;}
fixed_vector<T, capacity> &push_back(T val)
{
if (m_size >= capacity) {
fprintf(stderr, "Cannot enlarge fixed capacity vector!..");
return *this;
}
m_vd[m_size++] = val;
return *this;
}
friend std::ostream &operator<<(std::ostream &os, fixed_vector<T,
capacity> &r)
{
for (int i = 0; i < r.length(); ++i)
os << r.m_vd[i] << " ";
return os;
}
};
#endif
/***** Client Code *****/
fixed_vector<int, 5> ifv;
fixed_vector<char, 5) cfv(ifv);