从功能中获取价值 [英] Get value from function

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

问题描述

double Price() {
	double l, m, s;
	l = 3.50;
	m = 2.50;
	s = 1.50;
	return Price();
}

void Sprite(int& x) {
	string large, medium, small;
	string size;
	double a, b, c;
	a = Price(l);  <big>// im trying to get the value of l from int Price into a. </big>
	cout << "Large, Medium, or Small?" << endl;
	getline(cin, size);
	if ((size == "Large" || (size == "large")) { // ignore this part
		cout << "That will be" <<
	};
}

推荐答案

这段代码看起来像你猜测的那样,希望它可以在不考虑它的情况下工作:它不会。

为什么不呢?因为只要你打电话给Price,它就会立即调用Price,调用Price,直到你的应用程序耗尽堆栈空间并崩溃。



请坐下来考虑你的任务,尝试找出你需要做的事情,而不仅仅是猜测。从长远来看,这是一个更可行的策略。
That code looks like you guessed and hoped it might work without thinking about it: it won't.
Why not? Because as soon as you call Price, it immediately calls Price, which calls Price, which...until your application runs out of stack space and crashes.

Please, sit down, think about your task, and try to work out what you need to do instead of just guessing. It's a much more viable strategy in the long run.


这篇关于从功能中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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