2013-05-25 54 views
1

我做我自己的实现类的“矢量”使用模板的实践。除了Copy构造函数和overloaded =运算符,一切似乎都在工作。我不确定问题出在哪里。我会后下面的全部源代码(也我不知道如果我有任何的泄漏)错误与拷贝构造函数和平等的运营商

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

template <typename TT1> 
class Vector{ 
private: 
    int size; 
    TT1* ptr; 
public: 
    Vector(); 
    ~Vector(); 
    Vector(int size); 
    Vector(const Vector& a); 
    Vector<TT1>& operator=(const Vector& a); 
    void set(int position, TT1 data); 
    TT1 get(int position); 
    int get_size(); 
}; 

//------Default Constructor------ 
template <typename TT1> 
Vector<TT1> :: Vector() 
{ 
    size = 0; 
    ptr = NULL; 
} 

//-------Copy Constructor------- 
template <typename TT1> 
Vector<TT1> :: Vector(const Vector<TT1>& a) 
{ 
    if(a.ptr != NULL) 
    { 
     this->size = a.size; 
     delete[] ptr; 
     ptr = new TT1[size]; 
     for (int i = 0; i < this->size; i++) 
       this->ptr[i] = a.ptr[i]; 
    } 
} 

//------Destructor------- 
template <typename TT1> 
Vector<TT1> :: ~Vector() 
{ 
    delete[] ptr; 
} 

//-----Overloaded = operator---- 
template <typename TT1> 
Vector<TT1>& Vector<TT1> :: operator=(const Vector<TT1>& a) 
{ 
    if(this != &a) 
    { 
     this->size = a.size; 
     delete[] this->ptr; 
     this->ptr = new TT1[a.size]; 
     for (unsigned int i = 0; i < this->size; i++) 
       this->ptr[i] = a.ptr[i]; 
     return *this; 
    } 
} 
//----Constructor with size--- 
template<typename TT1> 
Vector<TT1> :: Vector(int size) 
{ 
    this->size = size; 
    ptr = new TT1[size]; 
} 

//---- Set data @ Poisition---- 
template<typename TT1> 
void Vector<TT1> :: set(int position, TT1 data) 
{ 
    *(ptr+ position) = data; 
} 

//----Get data From position---- 
template<typename TT1> 
TT1 Vector<TT1> :: get(int position) 
{ 
    return *(ptr + position); 
} 

//-----Get size---- 
template<typename TT1> 
int Vector<TT1> :: get_size() 
{ 
    return size; 
} 

void foo(Vector<string> a) 
{ 

} 

int main() 
{ 
    Vector<string> a(3); 
    a.set(0, "asd"); 
    a.set(2, "hjk"); 
    a.set(1, "34645!"); 
    for(int i = 0; i < a.get_size(); i++) 
    { 
     cout << a.get(i) << endl; 
    } 
    Vector<string> b = a; 
    for(int i = 0; i < a.get_size(); i++) 
    { 
     cout << b.get(i) << endl; 
    } 
    foo(a); 

    return 0; 
} 

而现在这里的错误是部分:

template <typename TT1> 
Vector<TT1> :: Vector(const Vector<TT1>& a) 
{ 
    if(a.ptr != NULL) 
    { 
     this->size = a.size; 
     delete[] ptr; 
     ptr = new TT1[size]; 
     for (int i = 0; i < this->size; i++) 
       this->ptr[i] = a.ptr[i]; 
    } 
} 

最后在这里:

//-----Overloaded = operator---- 
template <typename TT1> 
Vector<TT1>& Vector<TT1> :: operator=(const Vector<TT1>& a) 
{ 
    if(this != &a) 
    { 
     this->size = a.size; 
     delete[] this->ptr; 
     this->ptr = new TT1[a.size]; 
     for (unsigned int i = 0; i < this->size; i++) 
       this->ptr[i] = a.ptr[i]; 
     return *this; 
    } 
+0

+1对于写得很清楚的问题。 –

回答

1

您删除从未被分配的指针:

//-------Copy Constructor------- 
template <typename TT1> 
Vector<TT1> :: Vector(const Vector<TT1>& a) 
{ 
    std::cout << "Vector(const Vector<TT1>& a), " << a.size << std::endl; 
    if(a.ptr != NULL) 
    { 
     this->size = a.size; 
     delete[] ptr; //<-------------- no corresponding new 
     ptr = new TT1[size]; 
     for (int i = 0; i < this->size; i++) 
       this->ptr[i] = a.ptr[i]; 
    } 
} 

就评论说出来,it works fine

编辑:每迈克·西摩的观察,成员ESP。 ptr应该初始化:

Vector<TT1> :: Vector(const Vector<TT1>& a) : size(0), ptr(NULL) 
{ 
... 
} 

此外,虽然编译器接受它的构造函数/操作员=显然,如果你写形式的代码,否则将得到一个“不完全类型”错误

Vector(const Vector& a); 
Vector<TT1>& operator=(const Vector& a); 

相反,一致性和良好的作风恕我直言,包括模板参数:

Vector(const Vector<TT1>& a); 
Vector<TT1>& operator=(const Vector<TT1>& a); 

且类似地定义。

+0

这只是为了复制构造函数,对不对?我应该保持删除重载'='? – Bloodcount

+1

它工作正常...除非你复制一个空的向量。然后复制构造函数将成员保留为未初始化的,并且析构函数可能会爆炸。 –

+1

@Bloodcount是的,绝对 - 但只有当它的非空。你应该有'如果(这个 - > PTR)删除[]这个 - > PTR;'和'size'和'ptr'应该在初始化列表。 (后者是很好的做法,但不是必需的。)我相应地更新了代码示例。 –