如何处理和避免递归
How to handle and avoid Recursions
我正在使用自定义类来管理自动售货机。我不知道为什么它不断抛出堆栈溢出错误。我的程序有两个版本,第一个是通过预定义某些变量来查看类等是否工作的基本测试。第二个版本应该是这样的,每次运行程序时,有问题的变量都可以更改(取决于用户输入(。
如果有人能提出避免这种递归或堆栈溢出的方法,我会很棒。以下是所涉及的三个类的代码;
class Filling
{
protected:
vector<Filling*> selection;
string fillingChosen;
public:
virtual float cost()
{
return 0;
}
virtual ~Filling(void)
{
//needs to be virtual in order to ensure Condiment destructor is called via Beverage pointer
}
};
class CondimentDecorator : public Filling
{
public:
Filling* filling;
void addToPancake(Filling* customerFilling)
{
filling = customerFilling;
}
~CondimentDecorator(void)
{
delete filling;
}
};
class Frosted : public CondimentDecorator
{
float cost()
{ //ERROR IS HERE//
return (.3 + filling->cost());
}
};
下面是用于调用上述"成本"函数的代码;
void displayCost(Filling* selectedFilling)
{
cout << selectedFilling->cost() << endl;
}
下面是启动这一切的代码的一部分(main 方法(;
Filling* currentPancake = NULL;
bool invalid = true;
do
{
int selection = makeSelectionScreen(money, currentStock, thisState);
invalid = false;
if (selection == 1)
{
currentPancake = new ChocolateFilling;
}
else if...
.
.
.
.
else
invalid = true;
} while (invalid);
bool makingSelection = true;
CondimentDecorator* currentCondiment = NULL;
do
{
int coatingSelection = makeCoatingSelectionScreen(money, currentStock, thisState);
if (coatingSelection == 1)
currentCondiment = new Frosted;
else if (coatingSelection == 2)...
.
.
.
else if (coatingSelection == 0)
makingSelection = false;
currentCondiment = thisSelection;
currentCondiment->addToPancake(currentPancake);
currentPancake = currentCondiment;
displayCost(currentPancake);
//Below is the code that DOES work, however it is merely meant to be a test. The
//above code is what is needed to work, however keeps causing stack overflows
//and I'm uncertain as to why one version works fine and the other doesn't
/*currentCondiment = new Frosted;
currentCondiment->addToPancake(currentPancake);
currentPancake = currentCondiment;
displayCost(currentPancake);
currentCondiment = new Wildlicious;
currentCondiment->addToPancake(currentPancake);
currentPancake = currentCondiment;
displayCost(currentPancake);*/
} while (makingSelection);
displayCost(currentPancake);
delete currentPancake;
当您使用filling
也是Frosted
的Frosted
调用displayCost
时,会发生无限递归。这就在这里发生:
currentCondiment->addToPancake(currentPancake);
currentPancake = currentCondiment;
displayCost(currentPancake);
将currentCondiment
的filling
设置为 currentPancake
,然后使用 currentCondiment
调用displayCost
。
在此过程中,您还会泄漏最初分配给currentPancake
的内存。
顺便说一句,currentCondiment = thisSelection;
也会泄漏内存。
想法:使用像std::unique_ptr
这样的智能指针来消除泄漏。
相关文章:
- 通过递归进行因子分解
- 递归函数计算序列中的平方和(并输出过程)
- 使用递归的数组的最小值.这是怎么回事
- 递归列出所有目录中的C++与Python与Ruby的性能
- 递归计数给定目录的文件和所有目录
- 如何在BST的这个简单递归实现中消除警告
- C++:正在检查LinkedList中的回文-递归方法-错误
- 如何处理带有变体和使用的复杂性头递归
- 使用C++的递归线程处理会使资源暂时不可用
- 如何处理和避免递归
- 如何使用void类型处理递归回溯返回
- 以嵌套或递归方式(即在处理程序内)调用 asio io_service poll() 或 poll_one() 是否有效
- 0xC0000005中0x770115de处未处理的异常:使用递归矢量搜索读取位置0xccccccc0时发生访问冲突
- 使用BOOST _PP_SEQ_FOREACH _R递归处理枚举
- 从套接字递归处理数据
- 未处理的异常排序递归
- 试图在递归函数内部捕获失败的分配:未处理的异常/堆栈溢出
- 递归n皇后程序中c++分段错误的处理
- 用c++递归解决迷宫,不能处理异常情况
- 如何处理递归