【问题标题】:Class private member wont change in function类私有成员不会改变功能
【发布时间】:2021-07-30 05:20:43
【问题描述】:

using namespace std;

class map
{
    private:
        float result= 0;
    public:
        void func_1();
        void setres(float counter);
        float getres();
};
void map::setres(float counter)
{
    result= counter;
}
float map::getres()
{
    return result;
}
void map::func_1()
{
    float num_0=0, num_1=0, sum=0, i;
    
    map run;
    
    cout << run.getres() << " Result." << endl;
    if(result != 0)
        cout << "We already have a result saved and it's: " << run.getres() << endl;
    else
    {
        cout << "Give me first number: ", cin >> num_0;
        cout << "Give me second number: ", cin >> num_1;
        sum= num_0+num_1;
        cout << "Result is: " << sum << endl;
    }
    cout << "The program will save the result." << endl;
    run.setres(sum);
    cout << "The saved result is: " << run.getres() << "\nPress 1 to repeat the function and check\nif the result is saved." << endl;
    cin >> i;
    if(i==1)
        run.func_1();    
}

int main()
{
    map go;
    go.func_1();
    return 0;
}

不知道为什么私有变量result没有保存。我怎样才能让它发挥作用。

然后我开始编译它工作正常,私有结果正在改变,但然后我重新打开函数,结果回到 0,我希望它成为最后一个结果。

示例: 我放了4 我放了7 总和是 11 保存的结果是 11 然后我按 1 开始,结果又是 0,但我希望它是 11 而不是 0。

【问题讨论】:

    标签: c++ class scope member-access


    【解决方案1】:

    在函数中,您正在创建映射类型的局部变量

    map run;
    

    改变的数据成员结果。即该函数不会改变调用该函数的对象的数据成员结果。

    此外,例如在这段代码中,sn-p

    cout << run.getres() << " Result." << endl;
    if(result != 0)
    

    您正在访问两个不同对象的数据成员结果。在第一个语句中

    cout << run.getres() << " Result." << endl;
    

    在下一条语句中,您正在访问本地对象 run 的数据成员

    if(result != 0)
    

    您正在访问为其调用成员函数的对象(main 中声明的对象go)的数据成员结果。

    所以去掉函数中的声明

    map run;
    

    而不是像run.getres() 这样的表达式,只使用getres()this-&gt;getres()

    【讨论】:

      【解决方案2】:

      问题是您的函数没有使用调用该方法的对象的成员。相反,您在函数内创建一个新实例:

      void map::func_1()
      {
          float num_0=0, num_1=0, sum=0, i;
          
          map run;  // <---------- here
          //...
      

      这就是为什么每次调用函数都会得到一个新对象的原因。您不需要创建该实例。您已经在main 中创建了一个,并且在成员函数中您可以访问其成员。作为修复,您可以从代码中删除所有 run.。例如

      cout << run.getres() << " Result." << endl;
      

      ->

      cout << getres() << " Result." << endl;
      

      或者如果你喜欢

      cout << this->getres() << " Result." << endl;
      

      【讨论】:

        【解决方案3】:

        该值保存到run,并调用run.func_1() 进行检查,但随后在那里调用run.getres()。这个run 是新的map 对象,与保存数据的run 不同。

        您检查了result != 0,但您使用run.getres() 打印了结果。这里不一致。

        代替

            cout << run.getres() << " Result." << endl;
            if(result != 0)
                cout << "We already have a result saved and it's: " << run.getres() << endl;
        

        你应该这样做

            cout << result << " Result." << endl;
            if(result != 0)
                cout << "We already have a result saved and it's: " << result << endl;
        

            cout << getres() << " Result." << endl;
            if(getres() != 0)
                cout << "We already have a result saved and it's: " << getres() << endl;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-21
          • 1970-01-01
          • 2018-04-19
          • 1970-01-01
          • 1970-01-01
          • 2016-02-07
          • 2015-11-15
          • 1970-01-01
          相关资源
          最近更新 更多