首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

写Vector模版类,莫名有关问题

2012-07-15 
写Vector模版类,莫名问题在写《C++ PRIMER》上的Vector模版,照着http://topic.csdn.net/u/20090507/15/fa709

写Vector模版类,莫名问题
在写《C++ PRIMER》上的Vector模版,照着http://topic.csdn.net/u/20090507/15/fa709c9e-ca4a-4aa0-80df-b6dd4056ae09.html暂时写了这么多,但是出了不少问题。
重载的= 和 复制构造函数我用了不用的初始化方式,因为写的过程中两个方式出现不同错误:
使用重载操作符时,编译通过,但是会写入时内存冲突,运行中断
使用=时,报错:error C2662: “Vector<T>::size”: 不能将“this”指针从“const Vector<T>”转换为“Vector<T> &”
三行初始化新Vector的代码均是此错误。
然后,如果不使用这两个东西,Vector<int> ivec(5, 1)这样构造的话,copy_elem函数会报错:error C2100: 非法的间接寻址,Iter被实例化成了int,而不是*int
我实在是不知道肿么办了,希望大家能给我点思路,先行谢过!

C/C++ code
#include "stdafx.h"#include <iostream>#include <memory>#include <algorithm>using namespace std;template <typename T> class Vector {    typedef T* iterator;    typedef size_t size_type;public:    // 多种构造函数    Vector(): elements(0), first_free(0), end(0) { }    template <typename Iter> Vector(Iter, Iter); // 使用同类模版构造    Vector(size_type, const T&);    // 复制控制成员    Vector(const Vector&);    Vector& operator=(const Vector&);    ~Vector() { destory(); }    // 容器操作    void push_back(const T&);    void reserve(const size_t);    void resize(const size_t);    void resize(const size_t, const T&);    void display();    // ...    // 下标操作符    T& operator[](const size_t);    const T& operator[](const size_t) const;    // 迭代器    iterator begin() { return elements; }    iterator last() { return first_free; }    size_t size() { return first_free - elements; }    size_t capacity() { return end - elements; }private:    static allocator<T> alloc;  // 静态成员,所有Vector <T>对象可以公用,调用相应的成员函数分配不同的空间    void reallocate();    // get more space and copy existing elements    iterator elements;         // pointer to first element in the array    iterator first_free;         // pointer to first free element in the array    iterator end;                // pointer to one past the end of the array        void destory(); // 销毁元素及释放内存    template <typename Iter> void copy_elem(Iter, Iter);};// public:template <typename T> template <typename Iter> Vector<T>::Vector(Iter b, Iter e){    elements = alloc.allocate((e - b) * 3 / 2);    first_free = elements + (e - b);    end = elements + (e - b) * 3 / 2;    copy_elem(b, e);}template <typename T> Vector<T>::Vector(typename Vector<T>::size_type n, const T &t){    elements = alloc.allocate(n * 3 / 2);    first_free = elements + n;    end = elements + n * 3 / 2;    for (size_type i = 0; i != n; ++i)        alloc.construct(elements + i, t);}template <typename T> Vector<T>::Vector(const Vector<T> &vec){    // 初始化新Vector对象    elements = alloc.allocate(end - elements);    first_free = elements + (first_free - elements);    end = elements + (end - elements);    // 复制元素到新Vector    copy_elem(vec.elements, vec.first_free);}template <typename T> Vector<T>& Vector<T>::operator=(const Vector<T> &vec){    if (this != &vec) {        destory();        elements = alloc.allocate(vec.capacity());        first_free = elements + vec.size();        end = elements + vec.capacity();        copy_elem(vec.elements, vec.first_free);    }    return *this;}template <typename T> void Vector<T>::push_back(const T &t){    if (first_free == end)        reallocate();    alloc.construct(first_free, t);    ++first_free;}template <typename T> void Vector<T>::reserve(const size_t capa){    ptrdiff_t size = first_free - elements;    T* newelements = alloc.allocate(capa);    if (size < cape)        uninitialized_copy(elements, first_free, newelements);    else        uninitialized_copy(elements, elements + capa, newelements);    for (T *p = first_free; p != elements; )        alloc.destory(--p);    if (elements)        alloc.deallocate(elements, end - elements);    elements = newelements;    first_free = elements + min(size, capa);    end = first + capa;}template <typename T> void Vector<T>::resize(const size_t n){    size_t size = first_free - elements;    size_t capacity = end - elements;    if (n > capacity) {        reallocate();        uninitialized_fill(elements + size, elements + n, T());    } else if (n > size)        uninitialized_fill(elements + size, elements + n, T());    else {        for (T *p = first_free; p != elements + n; )            alloc.destroy(--p);    }    first_free = elements + n;}template <typename T> void Vector<T>::resize(const size_t n, const T &t){    size_t size = first_free - elements;    size_t capacity = end - elements;    if (n > capacity) {        reallocate();        uninitialized_fill(elements + size, elements + n, t);    } else if (n > size)        uninitialized_fill(elements + size, elements + n, t);    else {        for (T *p = first_free; p != elements + n; )            alloc.destroy(--p);    }    first_free = elements + n;}template <typename T> void Vector<T>::display(){    for (Vector<int>::iterator iter = begin(); iter != last(); ++iter)        cout << *iter << " ";    cout << endl;}template <typename T> T& Vector<T>::operator[](const size_t index){    return elements[index];}template <typename T> const T& Vector<T>::operator[](const size_t index) const{    return elements[index];}// private:template <typename T> allocator<T> Vector<T>::alloc;template <typename T> void Vector<T>::reallocate(){    // compute size of current array and allocate space for twice as many elements    ptrdiff_t size = first_free - elements;    ptrdiff_t newcapacity = 2 * max(size, 1);    T* newelements = alloc.allocate(newcapacity);    // construct copies of the existing elements in the new space    uninitialized_copy(elements, first_free, newelements);    for (T *p = first_free; p != elements; )        alloc.destroy(--p);    if (elements)        alloc.deallocate(elements, end - elements);    elements = newelements;    first_free = elements + size;    end = elements + newcapacity;}template <typename T> void Vector<T>::destory(){    for (T *p = first_free; p != elements; )        alloc.destroy(--p);    if (elements)        alloc.deallocate(elements, end - elements);}template <typename T> template <typename Iter> void Vector<T>::copy_elem(Iter b, Iter e){    for (typename Vector<T>::size_type i = 0; i != e - b; ++i)        alloc.construct(elements + i, *(b + i));}int main(){    Vector<int> ivec;        int i;    while (cin >> i)        ivec.push_back(i);    Vector<int> ivec2(ivec);    ivec2.display();    system("pause");    return 0;} 



[解决办法]
帮你改了一下复制构造函数,你原来的实现基本都是使用未初始化变量等一些常见错误,平时得注意养成好习惯。
C/C++ code
template <typename T> Vector<T>::Vector(const Vector<T> &vec){    elements = alloc.allocate(vec.size());    first_free = elements + vec.size();    end = elements + vec.size();    copy_elem(vec.elements, vec.first_free);}
[解决办法]
楼主,改的好辛苦,因为很多我也看不懂,不过总算调通了...因为其实我是在ubuntu下调的,ctrl+D没法停止cin>>i的输入,所以改成scanf了
C/C++ code
 #include "stdafx.h"#include <iostream>#include <memory>#include <algorithm>#include <cstdio>using namespace std;template <typename T> class Vector {    typedef T* iterator;    typedef size_t size_type;public:    // 多种构造函数    Vector(): elements(0), first_free(0), end(0) { }    template <typename Iter> Vector(Iter, Iter); // 使用同类模版构造    Vector(size_type, const T&);    // 复制控制成员    Vector(const Vector&);    Vector& operator=(const Vector&);    ~Vector() { destory(); }    // 容器操作    void push_back(const T&);    void reserve(const size_t);    void resize(const size_t);    void resize(const size_t, const T&);    void display();    // ...    // 下标操作符    T& operator[](const size_t);    const T& operator[](const size_t) const;    // 迭代器    iterator begin() { return elements; }    iterator last() { return first_free; }    size_t size() { return first_free - elements; }    size_t capacity() { return end - elements; }private:    static allocator<T> alloc;  // 静态成员,所有Vector <T>对象可以公用,调用相应的成员函数分配不同的空间    void reallocate();    // get more space and copy existing elements    iterator elements;         // pointer to first element in the array    iterator first_free;         // pointer to first free element in the array    iterator end;                // pointer to one past the end of the array        void destory(); // 销毁元素及释放内存    template <typename Iter> void copy_elem(Iter, Iter);};// public:template <typename T> template <typename Iter> Vector<T>::Vector(Iter b, Iter e){    elements = alloc.allocate((e - b) * 3 / 2);    first_free = elements + (e - b);    end = elements + (e - b) * 3 / 2;    copy_elem(b, e);}template <typename T> Vector<T>::Vector(typename Vector<T>::size_type n, const T &t){    elements = alloc.allocate(n * 3 / 2);    first_free = elements + n;    end = elements + n * 3 / 2;    for (size_type i = 0; i != n; ++i)        alloc.construct(elements + i, t);}template <typename T> Vector<T>::Vector(const Vector<T> &vec){    // 初始化新Vector对象    elements = alloc.allocate(end - elements);//这里不知道要不要加上vec.    first_free = elements + (vec.first_free - vec.elements);//--->加vec.    end = elements + (vec.end - vec.elements);//--->这里    // 复制元素到新Vector    copy_elem(vec.elements, vec.first_free);}template <typename T> Vector<T>& Vector<T>::operator=(const Vector<T> &vec){    if (this != &vec) {        destory();        elements = alloc.allocate(vec.capacity());        first_free = elements + vec.size();        end = elements + vec.capacity();        copy_elem(vec.elements, vec.first_free);    }    return *this;}template <typename T> void Vector<T>::push_back(const T &t){    if (first_free == end)        reallocate();    alloc.construct(first_free, t);    ++first_free;}template <typename T> void Vector<T>::reserve(const size_t capa){    ptrdiff_t size = first_free - elements;    T* newelements = alloc.allocate(capa);    if (size < capa)//--->拼写错误        uninitialized_copy(elements, first_free, newelements);    else        uninitialized_copy(elements, elements + capa, newelements);    for (T *p = first_free; p != elements; )        alloc.destory(--p);    if (elements)        alloc.deallocate(elements, end - elements);    elements = newelements;    first_free = elements + min((int)size, (int)capa);//--->这里转下类型    end = first_free + capa;//--->拼写错误?}template <typename T> void Vector<T>::resize(const size_t n){    size_t size = first_free - elements;    size_t capacity = end - elements;    if (n > capacity) {        reallocate();        uninitialized_fill(elements + size, elements + n, T());    } else if (n > size)        uninitialized_fill(elements + size, elements + n, T());    else {        for (T *p = first_free; p != elements + n; )            alloc.destroy(--p);    }    first_free = elements + n;}template <typename T> void Vector<T>::resize(const size_t n, const T &t){    size_t size = first_free - elements;    size_t capacity = end - elements;    if (n > capacity) {        reallocate();        uninitialized_fill(elements + size, elements + n, t);    } else if (n > size)        uninitialized_fill(elements + size, elements + n, t);    else {        for (T *p = first_free; p != elements + n; )            alloc.destroy(--p);    }    first_free = elements + n;}template <typename T> void Vector<T>::display(){    for (Vector<int>::iterator iter = begin(); iter != last(); ++iter)        cout << *iter << " ";    cout << endl;}template <typename T> T& Vector<T>::operator[](const size_t index){    return elements[index];}template <typename T> const T& Vector<T>::operator[](const size_t index) const{    return elements[index];}// private:template <typename T> allocator<T> Vector<T>::alloc;template <typename T> void Vector<T>::reallocate(){    // compute size of current array and allocate space for twice as many elements    ptrdiff_t size = first_free - elements;    ptrdiff_t newcapacity = 2 * max(size, 1);    T* newelements = alloc.allocate(newcapacity);    // construct copies of the existing elements in the new space    uninitialized_copy(elements, first_free, newelements);    for (T *p = first_free; p != elements; )        alloc.destroy(--p);    if (elements)        alloc.deallocate(elements, end - elements);    elements = newelements;    first_free = elements + size;    end = elements + newcapacity;}template <typename T> void Vector<T>::destory(){    for (T *p = first_free; p != elements; )        alloc.destroy(--p);    if (elements)        alloc.deallocate(elements, end - elements);}template <typename T> template <typename Iter> void Vector<T>::copy_elem(Iter b, Iter e){    for (typename Vector<T>::size_type i = 0; i != e - b; ++i)        alloc.construct(elements + i, *(b + i));}int main(){    Vector<int> ivec;        int i;    while (scanf("%d",&i))        ivec.push_back(i);    Vector<int> ivec2(ivec);    ivec2.display();    system("pause");    return 0;} 


[解决办法]
size_t size() { return first_free - elements; }
这个函数应该是const函数
size_t size() const { return first_free - elements; }

报错:error C2662: “Vector<T>::size”: 不能将“this”指针从“const Vector<T>”转换为“Vector<T> &”
这个错误就是因为它引起的
[解决办法]

探讨

楼主,改的好辛苦,因为很多我也看不懂,不过总算调通了...因为其实我是在ubuntu下调的,ctrl+D没法停止cin>>i的输入,所以改成scanf了
C/C++ code

#include "stdafx.h"
#include <iostream>
#include <memory>
#include <algorithm>
#include <cstdio>
using name……

[解决办法]
探讨

引用:

帮你改了一下复制构造函数,你原来的实现基本都是使用未初始化变量等一些常见错误,平时得注意养成好习惯。
C/C++ code

template <typename T> Vector<T>::Vector(const Vector<T> &amp;amp;vec)
{
elements = alloc.allocate(vec.size());
fir……

[解决办法]
好像用下类型转换才行吧。。。
C/C++ code
// #include "stdafx.h"#include <iostream>#include <memory>#include <algorithm>#include <cstdio>using namespace std;template <typename T> class Vector {    typedef T* iterator;    typedef size_t size_type;public:    // 多种构造函数    Vector(): elements(0), first_free(0), end(0) { }    template <typename Iter> Vector(Iter, Iter); // 使用同类模版构造    Vector(size_type, const T&);    // 复制控制成员    Vector(const Vector&);    Vector& operator=(const Vector&);    ~Vector() { destory(); }    // 容器操作    void push_back(const T&);    void reserve(const size_t);    void resize(const size_t);    void resize(const size_t, const T&);    void display();    // ...    // 下标操作符    T& operator[](const size_t);    const T& operator[](const size_t) const;    // 迭代器    iterator begin() { return elements; }    iterator last() { return first_free; }    size_t size() { return first_free - elements; }    size_t capacity() { return end - elements; }private:    static allocator<T> alloc;  // 静态成员,所有Vector <T>对象可以公用,调用相应的成员函数分配不同的空间    void reallocate();    // get more space and copy existing elements    iterator elements;         // pointer to first element in the array    iterator first_free;         // pointer to first free element in the array    iterator end;                // pointer to one past the end of the array        void destory(); // 销毁元素及释放内存    template <typename Iter> void copy_elem(Iter, Iter);};// public:template <typename T> template <typename Iter> Vector<T>::Vector(Iter b, Iter e){    elements = alloc.allocate((e - b) * 3 / 2);    first_free = elements + (e - b);    end = elements + (e - b) * 3 / 2;    copy_elem(b, e);}template <typename T> Vector<T>::Vector(typename Vector<T>::size_type n, const T &t){    elements = alloc.allocate(n * 3 / 2);    first_free = elements + n;    end = elements + n * 3 / 2;    for (size_type i = 0; i != n; ++i)        alloc.construct(elements + i, t);}template <typename T> Vector<T>::Vector(const Vector<T> &vec){    // 初始化新Vector对象    elements = alloc.allocate(vec.end - vec.elements);//现在确定要加上vec.    first_free = elements + (vec.first_free - vec.elements);//--->加vec.    end = elements + (vec.end - vec.elements);//--->这里    // 复制元素到新Vector    copy_elem(vec.elements, vec.first_free);}template <typename T> Vector<T>& Vector<T>::operator=(const Vector<T> &vec){    if (this != &vec) {        destory();        elements = alloc.allocate(vec.capacity());        first_free = elements + vec.size();        end = elements + vec.capacity();        copy_elem(vec.elements, vec.first_free);    }    return *this;}template <typename T> void Vector<T>::push_back(const T &t){    if (first_free == end)        reallocate();    alloc.construct(first_free, t);    ++first_free;}template <typename T> void Vector<T>::reserve(const size_t capa){    ptrdiff_t size = first_free - elements;    T* newelements = alloc.allocate(capa);    if (size < capa)//--->拼写错误        uninitialized_copy(elements, first_free, newelements);    else        uninitialized_copy(elements, elements + capa, newelements);    for (T *p = first_free; p != elements; )        alloc.destory(--p);    if (elements)        alloc.deallocate(elements, end - elements);    elements = newelements;    first_free = elements + min((int)size, (int)capa);//--->这里转下类型    end = first_free + capa;//--->拼写错误?}template <typename T> void Vector<T>::resize(const size_t n){    size_t size = first_free - elements;    size_t capacity = end - elements;    if (n > capacity) {        reallocate();        uninitialized_fill(elements + size, elements + n, T());    } else if (n > size)        uninitialized_fill(elements + size, elements + n, T());    else {        for (T *p = first_free; p != elements + n; )            alloc.destroy(--p);    }    first_free = elements + n;}template <typename T> void Vector<T>::resize(const size_t n, const T &t){    size_t size = first_free - elements;    size_t capacity = end - elements;    if (n > capacity) {        reallocate();        uninitialized_fill(elements + size, elements + n, t);    } else if (n > size)        uninitialized_fill(elements + size, elements + n, t);    else {        for (T *p = first_free; p != elements + n; )            alloc.destroy(--p);    }    first_free = elements + n;}template <typename T> void Vector<T>::display(){    for (Vector<int>::iterator iter = begin(); iter != last(); ++iter)        cout << *iter << " ";    cout << endl;}template <typename T> T& Vector<T>::operator[](const size_t index){    return elements[index];}template <typename T> const T& Vector<T>::operator[](const size_t index) const{    return elements[index];}// private:template <typename T> allocator<T> Vector<T>::alloc;template <typename T> void Vector<T>::reallocate(){    // compute size of current array and allocate space for twice as many elements    ptrdiff_t size = first_free - elements;    ptrdiff_t newcapacity = 2 * max(size, 1);    T* newelements = alloc.allocate(newcapacity);    // construct copies of the existing elements in the new space    uninitialized_copy(elements, first_free, newelements);    for (T *p = first_free; p != elements; )        alloc.destroy(--p);    if (elements)        alloc.deallocate(elements, end - elements);    elements = newelements;    first_free = elements + size;    end = elements + newcapacity;}template <typename T> void Vector<T>::destory(){    for (T *p = first_free; p != elements; )        alloc.destroy(--p);    if (elements)        alloc.deallocate(elements, end - elements);}template <typename T> template <typename Iter> void Vector<T>::copy_elem(Iter b, Iter e){    for (typename Vector<T>::size_type i = 0; i != e - b; ++i)        alloc.construct(elements + i, *(b + i));}int main(){    Vector<int> ivec((size_t)5,1);//---->转下类型        int i;    while (scanf("%d",&i))        ivec.push_back(i);    Vector<int> ivec2(ivec);    ivec2.display();    system("pause");    return 0;} 

热点排行