2013-02-06 34 views
0

嘗試cout Data [index]的返回值時出現錯誤。如果任何人都可以幫助我,這將是非常棒的。我通常知道這些錯誤是由分配的衝突內存引起的,或者有一個指針引用了一個已刪除的索引等。雖然我不刪除任何東西,但我不知道這個錯誤來自哪裏。cout期間發生堆錯誤C++

頭文件:

#pragma once 
#define INITIAL_CAPACITY 100 
#define CAPACITY_BOOST 40 


//Encapsulates the C-array 
template <typename DATA_TYPE> 
class Vector 
{ 
public: 
//Default/init-constructor hybrid 
Vector(int initialCapacity = INITIAL_CAPACITY) 
{ 
    Size=0; 
    Capacity = initialCapacity; 

    //Allocate the encapsulated C-array 
    Data= new DATA_TYPE[Size]; 
} 

//MUST HAVE A COPY-CONSTRUCTOR THAT PERFORMS deep-copy 
Vector(const Vector& copyFrom) 
{ 
    //Necessary to prevent assignment operator from crashing 
    //because it will attempt to Delete[] Data whe the Data pointer is garbage. 
    Data=NULL; 
    //Use assignment operator to perform the deep copy 
    *this = copyFrom; 
} 


//The class MUST have a destructor 
~Vector() 
{ 
    //Deallocate memory that our class has allocated 
    delete[] Data; 
} 
//MUST have an assignment operator that performs deep copy 
Vector& operator =(const Vector& copyFrom) 
{ 
    //0. Delete the old memory 
    delete[] Data; 
    //1. Copy size and Capacity 
    Size = copyFrom.Size; 
    Capacity = copyFrom.Capacity; 
    //2. Allocate Memory 
    Data = new DATA_TYPE[Capacity]; 
    //3. Copy elemenets 
    for(int i=0; i < Size; i++) 
     Data[i]= copyFrom.Data[i]; 

    //All assignment operators should return *this 
    return *this; 
} 

//Get accessors to return the values of Size and Capacity 
int GetSize() const 
{ 
    return this->Size; 
} 

int GetCapacity() const 
{ 
    return Capacity; 
} 

void Insert(int insertAt, const DATA_TYPE& newElement) 
{ 
    //**ASSIGNMENT** 
    //1. Determine if we have enough capacity for extra element(reallocate) 
    Size=GetSize(); 
    if(Size>=Capacity) 
    { 
     Capacity += CAPACITY_BOOST; 
    } 
    //Use a function to check bounds. 

    if((insertAt > Capacity)||(insertAt < 0)) 
    { 
     throw "Index is out of bounds"; 
    } 
    //2.Move the tail 
    for (int i=Size+1; i > insertAt; i--) 
     Data[i]=Data[i-1]; 

    //3.Insert element 
    Data[insertAt]= newElement; 

} 
//Inserts a new element at the end fo the Vector and increments the size 
void Add(const DATA_TYPE& newElement) 
{ 

    Insert(Size, newElement); 
    Size++; 
} 

void Remove(int index) 
{ 
    delete Data[index]; 
    for(i=index; i < Size-1; i++) 
     Data[i]=Data[i+1]; 
    Size--; 
    Capacity=Size; 
    //**ASSIGNMENT** 
    //Resize. Shrink vector when you have too much capacity 
    //TEST EVERYTHING 
} 

// Index operator 
DATA_TYPE operator[] (int index) const 
{ 
    // Check the bounds and throw an exception 
    if ((index < 0) || (index >= Size)) 
     throw "Error"; 

    return Data[index]; 
} 


private: 
//The count of actually used C-array elements 
int Size; 
//The count of the allocated C-array elements 
int Capacity; 
//The encapsulated C-array (pointer) 

DATA_TYPE* Data; 
}; 

主營:

#include <iostream> 
#include "vector.h" 
using namespace std; 

#define TEST_CAPACITY 100 
#define TEST_SIZE 10 

template<typename DATA_TYPE> 
void PassByValueTest(Vector<DATA_TYPE>passedByValue) 
{ 
} 
void main() 
{ 
//myVector is initialized using the default constructor 
Vector<int> myVector; 

//Populate myVector with some test values 
for (int i=0; i< TEST_SIZE; i++) 
    myVector.Add(i); 

//myOtherVector initialized using the init-constructor, initial capacity is 10 
//Vector<int> myOtherVector(TEST_CAPACITY); 


//Test by passing vector by value 
/* 
PassByValueTest(myVector); 

myVector = myOtherVector; 
*/ 
for(int i = 0; i < TEST_SIZE; i++) 
{ 
    cout << myVector[i]; 
} 
system("pause"); 
} 

回答

1

我想你應該切換:

Data= new DATA_TYPE[Size]; 

Data= new DATA_TYPE[Capacity]; 
+0

固定它,謝謝你!愚蠢的忽視。 –

1

你正在做Data = new DATA_TYPE[0];

Vector(int initialCapacity = INITIAL_CAPACITY) 
{ 
    Size=0;     // <<<--- 
    Capacity = initialCapacity; 

    //Allocate the encapsulated C-array 
    Data= new DATA_TYPE[Size]; // note Size is 0 
} 

然後訪問Data[i]是未定義的行爲:

for(int i = 0; i < TEST_SIZE; i++) 
{ 
    cout << myVector[i]; 
} 

側面說明,你應該INT從主迴歸,存在標準不void main

int main() 
{ 
} 
+0

分配一個大小爲0的數組沒有任何問題。您需要指出**問題在哪裏...... – thang

相關問題