私は今学期に初めての 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;
}