无效的分配大小 - 对象变量

时间:2013-07-22 01:44:23

标签: c++ class

我在本学期的第一个C ++中,并且在我之前在课堂上创建的MyVector课程中遇到了一些问题。我为对象创建了全局变量,这是我的老师所说的面向对象编程的“NO NO”。我相信我现在已经正确地声明了变量,但是自从我收到

以来
  

“无效的分配大小:4294967295字节。”在调用我的push_back时   功能

下面是我的代码(MyVector.h和MyVector.cpp),我知道使用using namespace std;不是最佳做法,但这是我老师想要的......我不知道为什么。

我已经介入了我的代码,无法确定下一步需要做什么。我有一种感觉,就是我之前声明的变量。它们以前是在更改之前全局声明的MyVector.cpp中。

//Declarations
int vSize;
int* myArray;
int startCap = 2;
const int TWO = 2;
const int ZERO = 0;

任何帮助或正确方向的观点都将受到赞赏。

提前致谢!

来自driver.cpp的电话

cout << "\nCreating a vector Sam of size 4.";
MyVector sam( 4 );

cout << "\nPush 12 values into the vector.";
for (int i = 0; i < 12; i++)
    sam.push_back(i);

MyVector.h

class MyVector
{

public:

    int vSize;
    int* myArray;
    int startCap;

    //Constructor
    MyVector ();
    MyVector (int n);

    //Deconstructor
    ~MyVector ();

    //Copy Constructor
    MyVector(const MyVector&);

    //Overloaded Assignment Operator
    MyVector& operator=(const MyVector&);

    //Getter Function: size
    //Purpose: Return the size of the vector
    //Return Type: int
    //Parameters: NONE
    int size () const;

    //Getter Funcation: capacity
    //Purpose: Return the capacity of the vector
    //Return Type: int
    //Parameters: NONE
    int capacity () const;

    //Setter Funcation: clear
    //Purpose: Clears the contents of the vector and sets the siz to zero and the capacity to two 
    //Return Type: void
    //Parameters: NONE
    void clear ();

    //Setter Funcation: push_back
    //Purpose: Adds integer to vector. If vector is not big enough double the vectors current capacity
    //Return Type: void
    //Parameters: int n
    void push_back (int n);

    //Getter Function: at
    //Purpose: Return value of emement at position n
    //Return Type: Int
    //Parameters: int n
    int at (int n) const;

    // overloaded << operator - a nonmember
    // make it a friend so it can see the array
    friend ostream& operator<<(ostream& out, const MyVector& s);
};

MyVector.cpp

//default constructors
MyVector::MyVector()
{
    int startCap = 2;
    int vSize = 0;
    myArray = new int[startCap];
}

MyVector::MyVector(int n)
{
    int startCap = n;
    int vSize = 0;
    myArray = new int[startCap];
}

//Deconstructor
MyVector::~MyVector()
{
    //deleting myArray and clearing it
    if (myArray != NULL)
    {
        delete [] myArray;
        myArray = NULL;
    }
}

// Copy constructor
// Purpose: Copy the data into this Array
// Parameters: a MyVector object
// Returns: none
MyVector::MyVector( const MyVector& v)
{
// Be sure that the string is not null
if ( v.myArray != NULL )
{
    // allocate storage and copy char array
    startCap = v.startCap;     
    //theStr = new char[strlen(b.theStr) + 1];
    myArray = new int[startCap];
    //strncpy(theStr, b.theStr, theStrLen );
    for (int i = 0; i < startCap; i++)
        myArray[i] = v.myArray[i];
}
else  // nothing to copy
{
    myArray = NULL;
    startCap = 0;
}
}

// The overloaded assignment operator
MyVector& MyVector::operator= (const MyVector& v)
{
// test for self-copy
if (this == &v)
   return *this;

// Consider two cases.
if (startCap >= v.startCap)  // there is room
{
    if (v.myArray != NULL)
    {
        for (int i = 0; i < startCap; i++)
        {
            this->myArray[i] = v.myArray[i];
        }
    }
    else // copying a null string
       myArray = NULL;

    startCap = v.startCap;
    return *this;
}
else  // not enough room
{
    // delete the original array
    delete [] myArray;

    startCap = v.startCap;
    if (startCap > 0) // okay, something to copy
    {
       // allocate the storage and copy
        myArray = new int[startCap + 1];
        for (int i = 0; i < vSize; i++)
        {
            this->myArray[i] = v.myArray[i];
        }
    }
   else // nothing to copy
      myArray = NULL;

   return *this;
   }
}

//Getter Function: size
//Purpose: Return the size of the vector
//Return Type: int
//Parameters: NONE
int MyVector::size() const
{
    return vSize;
}

//Getter Funcation: capacity
//Purpose: Return the capacity of the vector
//Return Type: int
//Parameters: NONE
int MyVector::capacity() const
{
    return startCap;
}

//Setter Funcation: clear
//Purpose: Clears the contents of the vector and sets the siz to zero and the capacity to two 
//Return Type: void
//Parameters: NONE
void MyVector::clear() 
{
    //clearing the array and setting the array to the default cap of 2 and size of 0
    if (myArray != NULL)
    {
        delete [] myArray;
        myArray = NULL;
    }

    vSize = 0;
    startCap = 2;
    int* myArray = new int[startCap];
}

//Setter Funcation: push_back
//Purpose: Adds integer to vector. If vector is not big enough double the vectors     current capacity
//Return Type: void
//Parameters: int n
void MyVector::push_back(int n)
{


//verifying the we are not writting the value
//past the capacity of the array
if(vSize + 1 > startCap)
{
    //Doubling the array size
    startCap = vSize * 2;
    //creating a temp array
    int* temp = new int[startCap];

    //for loop copying the contents of myArray to temp
    for (int i = 0; i < vSize; i++)
    {
        temp[i] = myArray [i];
    }

    //deleting the myArray
    delete[] myArray;
    //copying myArray from temp
    myArray = temp;
}

//finding the end of the array and incrementing and adding one to the array
myArray[vSize] = n;
vSize++;
}

//Getter Function: at
//Purpose: Return value of emement at position n
//Return Type: Int
//Parameters: int n
int MyVector::at(int n) const
{
    //If statment that returns value of the point in the array
    //or throws an error telling the user the index at which it failed
    if(n < vSize)
        return myArray[n];
    throw n;
}

ostream& operator<<(ostream& out, const MyVector& s)
{
    for (int i = 0; i < s.vSize; i++)
        out << s.myArray[i] << ' ';
    return out;
}

2 个答案:

答案 0 :(得分:2)

您正在构造函数中创建一个相同的变量并且clear,其名称与类中的名称相同,请对其进行初始化。 当你离开构造函数或清除主变量时,不要做任何改变。

它是一个初始化问题,尤其是构造函数

中的问题

清除功能

int* myArray = new int[startCap];

应该是

myArray = new int[startCap];

也在构造函数

  int startCap = n;
  int vSize = 0;

应该是

  startCap = n;
  vSize = 0;

答案 1 :(得分:2)

如果要分配给实例变量,请不要在名称上添加类型。这将创建一个与实例变量同名的局部变量,而不是分配给实际的实例变量,从而导致实例变量具有不正确的,甚至可能是未初始化的值。此问题出现在前两个构造函数和clear方法中。