获取堆栈溢出错误 [英] getting stack overflow error

查看:105
本文介绍了获取堆栈溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这现在为什么会出现堆栈溢出错误?

This is now getting a stack overflow error why?

#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
	const static int MAX_HOURS = 100;
	string firstName;
	string lastName;
	int hours[MAX_HOURS];
	int numhours;
	int wadges;
public:
	// First Name
	string getFirstName() { return firstName; }
	void setFirstName(string name) { firstName = name; }
	void retrieveFirstName() {
		string temp;
		cout << "First Name: ";
		cin >> temp;
		setFirstName(temp);
	}
	// Last Name
	string getLastName() { return lastName;	}
	void setLastName(string name) {	lastName = name; }
	void retrieveLastName() {
		string temp;
		cout << "Last Name: ";
		cin >> temp;
		setLastName(temp);
	}
	// Num hours
	int getNumhours() { return numhours; }
	void setNumhours(int hours) {
		if (hours >= 0 && hours <= MAX_HOURS) {
			numhours = hours;
		} else { 
			numhours = 0;
		}
	}
	void retrieveNumhours() {
		cout << "How many hours? ";
		int temphours;
		cin >> temphours;
		setNumhours(temphours);
	}
	// wadges
	int getwadges() {return wadges; }
	void setwadges(int twadges) {
	
}
	void retrievewadges() {
	int twadges;
	cout << "What is your hourly wadge? " ;
	cin >> twadges;
	setwadges(twadges);
	}
		

	void retrieve() {
		retrieveFirstName();
		retrieveLastName();
		retrieveNumhours();
		retrievewadges();
	}
	double calwadges() {
	double total = 0;
	for (int i = 0; i < getNumhours(); i ++) {
		total += hours[i];	
	}
	return calwadges();
	
	}
	
	
	
};

int main() {
	Employee name;
	name.retrieve();
	cout << "Grosspay: " << name . calwadges() << endl;
	return 0;
}

推荐答案

由于某种原因,您从自身内部调用了相同的函数,从而创建了无限循环.

Because for some reason your calling the same function from within itself creating an infinite loop.

double calwadges() {
    double total = 0;
    for (int i = 0; i < getNumhours(); i ++) {
        total += hours[i];
    }
    
    //Calling this function from within itself, which will then call itself
    //again and so on
    return calwadges();
  }



堆栈基本上是您获取代码中当前点所经过的所有功能的列表,因此您的堆栈有时可能类似于:main() -> myfunction() -> myotherfunction()
只有有限的空间可用于存储此列表",因此,当您进行无限递归(永远调用自身的函数)时,最终将用光空间来存储该函数调用,这就是您的堆栈溢出错误. br/> 这是一个非常简化的解释,因此,如果您感兴趣的话,应该对堆栈的工作方式及其用途进行快速搜索.



The stack is basically a list of all the functions you''ve come through to get the current point in the code, so your stack might at some point be something like: main() -> myfunction() -> myotherfunction()
There is only a limited amount of space available to store this ''list'' so when you have infinite recursion (a function calling itself forever) then you eventually run out of space to store that function call which is your stack overflow error.
This is a very simplified explanation so if your interested you should do a quick search on how the stack works and what its purpose is.


return calwadges()-------- ->是此错误的原因.
希望你写错了.
return calwadges() ---------> is the reason for this error.

Hope u wrote this by mistake.


这篇关于获取堆栈溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆