Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法在带有参数的函数的类中分离输入函数_C++_Constructor - Fatal编程技术网

C++ 无法在带有参数的函数的类中分离输入函数

C++ 无法在带有参数的函数的类中分离输入函数,c++,constructor,C++,Constructor,无法在此问题中分离输入函数 在这段代码中,我无法分离输入函数。然而,当我将输入放入int main()时,它显示了正确的解决方案。我的代码有什么问题 #include<iostream> using namespace std; class test { int a,b; public: void input(); void swapValue(int value1, int value2); }; void test::input() { c

无法在此问题中分离输入函数

在这段代码中,我无法分离输入函数。然而,当我将输入放入int main()时,它显示了正确的解决方案。我的代码有什么问题

#include<iostream>
using namespace std;
class test
{
    int a,b;
    public:
    void input();
    void swapValue(int value1, int value2);
};
void test::input()
{
    cin>>a>>b;
}
void test::swapValue(int value1, int value2)
{
    cout << "Swap value in function" << endl;

    int temp = value1;
    value1   = value2;
    value2   = temp;
    cout << "value 1: " << value1 << endl;
    cout << "value 2: " << value2 << endl;
}
int main()
{
    int a,b;
    test s1;
    s1.input();
    cout << "===============================" << endl;
    cout << "After the function call" << endl;
    s1.swapValue(a, b);

    system("pause");
    return 0;
}
#包括
使用名称空间std;
课堂测试
{
INTA,b;
公众:
无效输入();
无效swapValue(int value1,int value2);
};
void测试::输入()
{
cin>>a>>b;
}
无效测试::swapValue(int-value1,int-value2)
{
库特
input
中的局部变量
a
b
input
函数的局部变量,一旦程序从函数返回,它们就不再存在,尽管它们与
main
中的变量
a
b
具有相同的名称

这是最基本的知识,将在编程课本的第一章中解释

您只需在
input
中删除此行:

void test::input()
{
    // delete this line:   int a,b;
    cin >> a >> b;
}
顺便说一句:
swapValue
的用途我不太清楚,但基本上你似乎混淆了类成员和局部变量。

在这个函数中:

void test::input()
{
    int a, b;
    cin >> a >> b;
}
您没有更改
test
a
b
成员字段。您正在创建新的
a
b
变量,这些变量在函数结束时被丢弃,成员字段保持不变。请删除
int a,b;
行以修复此问题

另一个问题是,你不能阅读
a
b
swapValue
对它们没有任何作用,那么为什么它甚至是一个成员函数呢?你必须将
a
b
的引用传递给
swapValue
,但它们是私有的,没有getter,所以这不起作用。我推荐一些方法g改为:

void test::swapValue()
{
    cout << "Swap value in function" << endl;
    int temp = a;
    a = b;
    b = temp;
    cout << "value 1: " << a << endl;
    cout << "value 2: " << b << endl;
}

现在它是一个交换
a
b
字段的成员函数,因此,它不需要任何参数。

哦,我忘了把它放在输入函数中,但看起来程序仍然显示错误的答案,你能试着运行看看吗?@PeterNguyen你需要回答你的问题,并向我们展示一个输入和输出的示例预期输出。
void-swapValue(int-value1,int-value2);
现在也有同样的问题:
value1
value2
也是局部变量,修改它们不会修改任何其他内容。请告诉我们
swapValue
到底应该做什么?给我们一个输入和预期输出的示例。
void test::swapValue()
{
    cout << "Swap value in function" << endl;
    int temp = a;
    a = b;
    b = temp;
    cout << "value 1: " << a << endl;
    cout << "value 2: " << b << endl;
}
s1.swapValue();