gpt4 book ai didi

c++ - 分配大小无效 - 对象变量

转载 作者:行者123 更新时间:2023-11-30 01:58:11 25 4
gpt4 key购买 nike

我这学期第一次接触 C++,我在类里面创建的 MyVector 类遇到了一些问题。正如我的老师对面向对象编程所说的那样,我为对象创建了全局变量,这是“不可以”。我相信我现在已经正确地声明了变量,但是自从我收到一个

"Invalid allocation size: 4294967295 bytes." When calling my push_back function.

下面是我的代码(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);
};

我的 vector .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;
}

最佳答案

你在构造函数中创建了一个相同的变量,并且清除了,它与类中的变量同名,初始化它。当您离开构造函数或清除时,主要变量不会发生任何变化。

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

清晰的函数

int* myArray = new int[startCap];

应该是

myArray = new int[startCap];

也在构造函数中

  int startCap = n;
int vSize = 0;

应该是

  startCap = n;
vSize = 0;

关于c++ - 分配大小无效 - 对象变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17778789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com