0

Data[index] の戻り値をカウントしようとするとエラーが発生します。誰かが私を助けることができれば、それは素晴らしいことです. 通常、これらのエラーは、競合するメモリが割り当てられているか、ポインタが削除されたインデックスを参照していることが原因であることがわかっています。ただし、何も削除していないため、このエラーの原因はわかりません。

ヘッダー ファイル:

#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");
}
4

2 に答える 2

1

私はあなたが切り替える必要があると思います:

Data= new DATA_TYPE[Size];

Data= new DATA_TYPE[Capacity];
于 2013-02-06T03:47:09.427 に答える
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()
{
}
于 2013-02-06T03:48:19.523 に答える