我创建了一个函数来创建 cows 变量,然后我创建了另一个函数来屠杀它们。这两个函数是类中的方法。当我使用 create cow 函数时,它起作用了,我得到了 cows 的值。但是,当我访问屠夫函数时,奶牛的值为零。
这就像有两个不同的变量;我不认为函数如果是 void 函数就可以有局部变量。
我该如何解决这个问题?
#include <string>
using namespace std;
class Cheif
{
int points;
string name;
protected:
int NoodleS;
int RosemaryS;
int ParcleyS;
int PepperS;
int Field;
int Water;
public:
int star;
int foodPrep;
int ingrPrep;
int endOfPrep;
int money;
int ingr1;
int ingr2;
int ingr3;
int Noodle;
int Rosemary;
int Parcley;
int Pepper;
int chickMeat;
int beef;
int chickens;
int cows;
// constructor
Cheif()
{
money = 1000;
points = 0;
star = 10;
endOfPrep = 0;
ingr1 = 0;
ingr2 = 0;
ingr3 = 0;
Noodle = 0;
Rosemary = 0;
Parcley = 0;
Pepper= 0 ;
cows = 0;
chickens = 0;
beef = 0;
chickMeat = 0;
}
// method
//////////////////////////////////////////////////
// //
// ask user for their name //
// //
//////////////////////////////////////////////////
void FindName()
{
cout << "what is your name? " << endl;;
cin >> name;
cout << endl << " Welcome " << name
<< " let us begin... " <<endl;
}
//////////////////////////////////////////////////
// END //
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// //
// Buy animal live stock //
// //
//////////////////////////////////////////////////
void Buy_Cow()
{
if(money > 199)
{
money -= 200;
cows += 1;
cout <<" you now have " << cows << " cows" << endl;
}
}
void Buy_Chick()
{
if(money > 99)
{
money -= 200;
chickens += 1;
}
}
//////////////////////////////////////////////////
// END //
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// //
// if user goes to open store //
// //
//////////////////////////////////////////////////
void GOTO_Store()
{
// while food is not prepared yet
while(endOfPrep == 0){
PREPAREMEAL:
// show menu
cout << "<1> Make Food \n"
<< "<2> Collect Ingridents \n"
<< "<3> Butcher Animal \n"
<< "<4> Go Back... \n" << endl;
// create variable to hold choice
string OS_Choice;
cin >> OS_Choice;
/////////// if user decides to "make food" /////////////
if (OS_Choice == "1")
{
if(foodPrep == 1)
{
cout << "you've already prepared the meal..." << endl;
}else{
if(ingr1 <= 0 ||ingr2 <= 0 || ingr3 <= 0)
{
goto PREPAREMEAL;
}else{
cout << "your using" << ingr1 << " " << ingr2<< " " << ingr3 << endl;
} // end of ingredient check
cout << " and how shall this mean be prepared? " << endl;
int prepMethod;
cout << "<1> Baked \n "
<< "<2> boiled \n "
<< "<3> Fried \n "
<< "<4> mixed \n ";
cin >> prepMethod;
cout << " And what kind of sides would you like to add? " << endl;
int sideChoice;
cout << "<1> bread Roll \n "
<< "<2> Rice \n "
<< "<3> Beans \n" << endl;
cin >> sideChoice;
foodPrep = 1;
}// end of food choice
//begin food compare.
/////////// if user decides to get ingrediants /////////////
}else if(OS_Choice == "2"){
if (ingrPrep == 1){
cout << " you have already collected the ingredients " << endl;
}else{
cout << "what 3 ingridents will you use? " << endl;
cin >> ingr1;
cin >> ingr2;
cin >> ingr3;
}// end of ingrident prep
/////////// if user decides to get ingrediants /////////////
}else if(OS_Choice == "3")
{
cout << " you have " << cows << " cows \n "
<< " you have " << chickens << " Chickens \n ";
cout << "what would you like to butcher? " << endl;
cout << "<1> Cows : " << cows << endl;
cout << "<2> Chicken : " << chickens << endl;
int B_Choice;
cin >> B_Choice;
if(B_Choice == 1){
if(cows == 0){
cout << " sorry you dont have any cows " << endl;
}else{
cows = cows - 1;
beef = beef + 5;
cout << " you now have " << beef << "peices of cow meat" << endl;
ingrPrep = 1;
}//end of cow check
}else if(B_Choice == 2){
if(chickens == 0){
cout << " sorry you dont have any chickens " << endl;
}else{
chickens = chickens - 1;
chickMeat = chickMeat + 2;
cout << " you now have " << chickMeat << "peices of Chicken meat" << endl;
ingrPrep = 1;
} // end chicken Check
}else {
cout << "invalid Choice" << endl;
}// end of b choice
}else if(OS_Choice == "4") {
endOfPrep = 1;
foodPrep = 0;
ingrPrep = 0;
ingr1 = 0;
ingr2 = 0;
ingr3 = 0;
}// end of ingr prep.
}//end of while loop
}
//////////////////////////////////////////////////
// END //
//////////////////////////////////////////////////
};
最佳答案
您的类的构造没有明显的错误会导致您所描述的问题。 (要点 unwind 说明将方法实现直接放在类声明中通常不是一个好主意是正确的,但这不是问题的原因。)
事实上,如果我在您发布的代码末尾添加一个简单的 main():
int main() {
Cheif c;
c.Buy_Cow();
c.GOTO_Store();
}
并编译并运行它,它给出了预期的结果(cows=1 的值)。
所以,这告诉您问题不在于此类,而在于您如何从程序的其余部分调用它。
要追踪到这一点,可以使用常用的调试技术。例如:
- 尝试确定导致问题的特定事件序列
- 使用调试器和/或打印语句找出问题所在
关于c++ - 为什么 int 变量在这个函数中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3167174/