【发布时间】:2015-05-07 02:29:15
【问题描述】:
我在 C++ 中遇到了一个问题,我调用了一个为事物分配一些值的函数,但是这些分配在函数完成后丢失了。这是我的代码:
#include <iostream>
#include <string>
using namespace std;
void Input(string a, string b){
cout << "Input a: \n";
cin >> a;
cout << endl;
cout << "Input b: \n";
cin >> b;
cout << endl << "Inputen Strings (still in the called function): \n";
cout << a << " " << b << endl << endl;
};
int main(){
string c = "This didn't";
string d = "work";
Input(c,d);
cout << "Inputen Strings (now in the main function): \n";
cout << c + " " + d << endl;
return 0;
};
所以每当我运行它时,(输入“Hello”然后输入“World”)程序运行如下:
输入一个:
你好
输入 b:
世界
Inputen Strings(仍在被调用函数中):
你好世界
Inputen 字符串(现在在 main 函数中):
这没用
我不知道为什么它只是暂时保存值。任何帮助表示赞赏!
【问题讨论】:
标签: c++ function variable-assignment temporary