此时所有代码都在减去我的析构函数。当我调用它时,我得到一个“Debug Assertion Failed!”
第52行
表达式:_BLOCK_TYPE_IS_VALID(p_Head-> nBlockUse)
这是针对一小时到期的作业。我试图在没有任何运气的情况下自己解决这个问题。它发生在运行时,并且在我的进程运行完成并在系统上等待(“PAUSE”)之后;
#pragma once
#include "stdafx.h"
#include <array>
#include <stdexcept>
#include <iostream>
#include <string>
// class OutOfBoundsException
// Thrown when an input is outside the availabe range of an array.
// Tells the user what the error was, and the offending parameter
class OutOfBoundsException
{
private:
int m_input;
std::string m_error;
public:
OutOfBoundsException(int input)
{
m_input = input;
m_error = "Out of bounds: ";
}
int getInput() const
{
return m_input;
}
std::string getError() const
{
return m_error;
}
};
class MyVector
{
private:
// create constant of default capacity set to two and a const BLANK for a blank array
const int DEFAULT_CAPACITY = 2;
const int BLANK = 0;
// create variables to store the current capacity and the current size
int currentCapacity;
int currentSize;
// create a global pointer for the array
int* myArrayPtr;
public:
// MyVector()
// Default Constructor
// Sets values per default
MyVector()
{
// sets currentCapacity to default capacity
currentCapacity = DEFAULT_CAPACITY;
// sets currentSize to blank
currentSize = BLANK;
// creates a dynamic array using default parameters above
myArrayPtr = new int[currentCapacity];
}
// MyVector(int n)
// Constructor that takes an input for the capacity of the array
MyVector(int n)
{
// sets currentCapacity to n
currentCapacity = n;
// sets currentSize to blank
currentSize = BLANK;
// creates a dynamic array using parameters above
myArrayPtr = new int[currentCapacity];
}
// ~MyVector()
// Destructor that cleans up memory allocations within class
~MyVector()
{
// deletes dynamic array
delete[] myArrayPtr; // the heap is happy
}
// size function
// Purpose: returns the current size of the array
// Parameters: N/A
// Returns: currentSize
int size() const
{
// returns the size of the array << currentSize
return currentSize;
}
// capacity function
// Purpose: returns the current capacity of the array
// Parameters: N/A
// Returns: currentCapacity
int capacity() const
{
// returns the current capacity of the array << currentCapacity
return currentCapacity;
}
// clear function
// Purpose: deconstructs the array, then reconstructs a default array
// Parameters: N/A
// Returns: N/A
void clear()
{
// sets all elements back to defaults
delete[] myArrayPtr;
currentCapacity = DEFAULT_CAPACITY;
currentSize = BLANK;
myArrayPtr = new int[currentCapacity];
}
// push_back function
// Purpose: pushes n into the back of the array, if the array is too small to hold another value, it doubles the size of the array.
// Parameters: int n
// Returns: N/A
void push_back(int n)
{
if (currentSize == currentCapacity)
{
if (currentCapacity == BLANK)
{
clear();
}
else
{
// create a local array that will be destroyed as soon as
int* temp = new int[currentCapacity * DEFAULT_CAPACITY];
for (int i = 0; i < currentCapacity; i++)
{
temp[i] = myArrayPtr[i];
}
delete[] myArrayPtr;
currentCapacity *= DEFAULT_CAPACITY;
myArrayPtr = temp;
}
}
if (currentSize != BLANK)
{
myArrayPtr[currentSize] = n;
}
else
{
myArrayPtr[currentSize] = n;
}
currentSize++;
}
// at function
// Purpose: returns value at n
// Parameters: int n
// Returns: myArrayPtr[n]
// Throws OutOfBoundsException if n is greater than the index of the last item in the array
int at(int n) const
{
try
{
// returns the value at n
if (n > currentSize - 1)
throw OutOfBoundsException(n);
else
return myArrayPtr[n];
}
catch (OutOfBoundsException e)
{
std::cout << std::endl << e.getError() << e.getInput() << std::endl;
}
return myArrayPtr[n];
}
};
导致此错误的代码是一个简单的跑步者类,仅用于测试它。
#pragma once
#include "stdafx.h"
#include "MyVector.h"
#include <iostream>
using namespace std;
int main()
{
MyVector hello(10);
// hello.~MyVector();
hello.push_back(-1);
hello.push_back(-1);
hello.push_back(-1);
hello.push_back(-1);
hello.push_back(0);
hello.~MyVector();
system("PAUSE");
return 0;
}
答案 0 :(得分:0)
析构函数被调用两次。 hello.~MyVector()
是第一个。 : - )
答案 1 :(得分:0)
您的MyVector对象不是由您自己分配的,因此程序会自动将其删除。如果你想删除它,只需自己分配:
MyVector *mv = new Myvector(...);
[...]
delete mv;
return ;
delete将自动调用对象析构函数。
顺便说一句,http://www.cplusplus.com/articles/y8hv0pDG/
Coplien就是这样,即使你不需要它们,也要习惯于定义它们。