1

这学期我是第一个 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);

我的向量.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);
};

我的矢量.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;
}
4

2 回答 2

2

您正在构造函数中创建一个相同的变量并清除,它具有相同的名称是类中的变量,初始化它。并且当您离开构造函数或清除主要变量时,不要进行任何更改。

这是一个初始化问题,尤其是构造函数中的问题

在明确的功能

int* myArray = new int[startCap];

应该

myArray = new int[startCap];

也在构造函数内部

  int startCap = n;
  int vSize = 0;

应该

  startCap = n;
  vSize = 0;
于 2013-07-22T02:06:31.040 回答
2

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

于 2013-07-22T02:14:04.013 回答