我创建了一个创建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 //
//////////////////////////////////////////////////
};
答案 0 :(得分:1)
方法是否具有返回类型void
并不重要;如果他们属于同一个类,他们将使用类'实例变量。
但你的方法看起来不像方法;在他们的名字之前没有类名。这些是在类的声明中定义的吗?
答案 1 :(得分:1)
你班级的建设没有明显的错误,这会导致你所描述的问题。 (点展开使得将方法实现直接放在类声明中通常不是一个好主意,但这不是问题的原因。)
事实上,如果我在你发布的代码的末尾添加一个简单的main():
int main() {
Cheif c;
c.Buy_Cow();
c.GOTO_Store();
}
并编译&amp;运行它,它给出了预期的结果(牛的值= 1)。
所以,这告诉你问题不在于这个课程,而是你从你的其他课程中调用它的方式。
要跟踪它,通常的调试技术适用。例如: