C++ 如何更改私有类内部结构中的数组?(c+;+;)
这才刚刚演变成一场激烈的战争。如果我不明白的话,我以后会再问这个问题 这些值在函数内部更改,但一旦函数结束,这些值将返回到最初设置的值C++ 如何更改私有类内部结构中的数组?(c+;+;),c++,arrays,class,private,C++,Arrays,Class,Private,这才刚刚演变成一场激烈的战争。如果我不明白的话,我以后会再问这个问题 这些值在函数内部更改,但一旦函数结束,这些值将返回到最初设置的值 class inventory { private: struct instock { double everything[4]; //Gets set to 0-3. Then user changes values. }; public: instock stock; void changegas(double userinput); voi
class inventory
{
private:
struct instock {
double everything[4]; //Gets set to 0-3. Then user changes values.
};
public:
instock stock;
void changegas(double userinput);
void stocking(); //populates the array
void output(); //outputs the array
};
void inventory::stocking(){
对于(inti=0;i您正在通过值传递backroom。您需要更改菜单()
接受资源清册引用或资源清册指针,最好是前者。这是因为当您按值传递类对象时,整个对象将复制到被调用函数的本地新实例,对该实例所做的任何更改都不会影响原始副本
这应该起作用:
void menu(int zed, inventory& a) {
int z = zed; //you don't need this by the way
int c = 0;
switch (z){//start switch
case 1:
cout << "Enter the amount of Gas you want to change: ";
cin >> c;
a.changegas(c);
break;
case 2:
cout << "Enter the amount of Tires you want to change: ";
cin >> c;
a.changetires(c);
break;
case 3:
cout << "Enter the amount of Soda you want to change: ";
cin >> c;
a.changesoda(c);
break;
case 4:
cout << "Enter the amount of Snacks you want to change: ";
cin >> c;
a.changesnacks(c);
break;
}
}
void菜单(int-zed、inventory&a){
int z=zed;//顺便说一下,您不需要这个
int c=0;
开关(z){//启动开关
案例1:
cout>c;
a、 煤气(c);
打破
案例2:
cout>c;
a、 更换轮胎(c);
打破
案例3:
cout>c;
a、 苏打(c);
打破
案例4:
cout>c;
a、 (三);
打破
}
}
您对输入/输出有什么特别的问题/期望?详细说明(编辑您的问题!!),很不清楚你要的是什么!在foo::changebar
中的bar
确实是this->bar
。所以这些值应该被更改。与结构问题的声明位置无关。好吧..删除了我的答案..一些白痴决定无缘无故地否决投票..不管怎样,只要看看ideone链接。您的代码没有问题。bar的值确实发生了变化。好的,其他地方一定有问题。谢谢您的帮助。谢谢!如果可以的话,我会给您一个+1。
int main()
{
backroom.stocking();
int choice;
cout << "What would you like to change? \n1.)Gas\n2.)Tires\n3.)Soda\n4.)Snacks"<<endl;
cin >> choice;
menu(choice, backroom);
backroom.output();
return 0;
}
void menu(int zed, inventory a){
int z = zed;
int c = 0;
switch (z){//start switch
case 1:
cout << "Enter the amount of Gas you want to change: ";
cin >> c;
a.changegas(c);
break;
case 2:
cout << "Enter the amount of Tires you want to change: ";
cin >> c;
a.changetires(c);
break;
case 3:
cout << "Enter the amount of Soda you want to change: ";
cin >> c;
a.changesoda(c);
break;
case 4:
cout << "Enter the amount of Snacks you want to change: ";
cin >> c;
a.changesnacks(c);
break;
break;
}//endswitch
}// end funct
void menu(int zed, inventory& a) {
int z = zed; //you don't need this by the way
int c = 0;
switch (z){//start switch
case 1:
cout << "Enter the amount of Gas you want to change: ";
cin >> c;
a.changegas(c);
break;
case 2:
cout << "Enter the amount of Tires you want to change: ";
cin >> c;
a.changetires(c);
break;
case 3:
cout << "Enter the amount of Soda you want to change: ";
cin >> c;
a.changesoda(c);
break;
case 4:
cout << "Enter the amount of Snacks you want to change: ";
cin >> c;
a.changesnacks(c);
break;
}
}