【问题标题】:unable to call getter method无法调用getter方法
【发布时间】:2013-10-19 03:18:26
【问题描述】:

我试图在用户输入后获取 x 和 y 的值, 将值放在构造函数中, 并在Point.cpp 中使用getX()getY() 方法进行一些计算,但问题是,它总是返回X:0 Y:0,我不知道为什么。我的代码如下

Point.cpp

#include <iostream>
#include "Point.h"
using namespace std;

     Point::Point(int x,int y)  {
        setX(x);
        setY(y);
     } 

     int Point::getX() {
       return x;
     }

     int Point::getY() {
       return y;
     }

     void Point::setX(int x) {
       this->x = x;
     }

     void Point::setY(int y) {
      this->y = y;
    } 

    void Point::someMethods() {
       x = getX();
       y = getY();
       cout << "X:" << x << "Y:" << y;
       // do some methods here after getting the x and y cords;
    }

点.h

 #ifndef Point_Point_h
 #define Point_Point_h

class Point {
  private:
         int x,y;
  public : 

       Point() {
       x = 0;
       y = 0;
       }//default consrructor

      Point(int x,int y);
      int getX();
      int getY();
      void setX(int x);
      void setY(int y);
      void someMethods();
 };    

#endif

【问题讨论】:

  • Point Point(x,y); 替换为point.setX(x);point.setY(y);(在当前示例中,您从未真正修改过point)。
  • 感谢您的帮助。设法理解它并在你们的帮助下得到它

标签: c++ visual-c++ c++11 c++-cli c++builder


【解决方案1】:

您需要注意您使用了point.someMethods(),而实际上从未将point 更改为使用x 和y。

int x,y;
int main()
{     
    cout << "Please Enter x-Cordinate"<< endl;
    cin >> x;
    cout << "Please Enter y-Cordinate" << endl;
    cin >> y;

    Point point(x,y);

    //just putting here in main to show that X and Y value isn't passed
    point.someMethods(); <-- the output will always be X:0 Y:0 no matter what value the user input
}

会起作用,因为现在该点是用 x 和 y 创建的(请注意,我已经从 main 函数之前删除了点声明。您也可以在那里声明它并使用它的 set 函数)。

【讨论】:

  • 感谢您的帮助。设法理解它并在你们的帮助下得到它
【解决方案2】:

这是因为您的程序中有 2 个“Point”对象。一个在这里实例化:“Point point;”另一个在这里实例化:“Point Point(x,y);”。

最后你调用的是“point.someMethods();”使用第一个对象,该对象是使用默认构造函数构造的,因此将 x 和 y 设置为 0。

我相信在这种情况下你应该删除“Point point;”从全局命名空间实例化,并更改“Point Point(x,y);”的名称到“点点(x,y);”。然后它会按预期工作:

#include <iostream> 
#include "Point.h"
using namespace std;
int x,y;

int main()
{     
    cout << "Please Enter x-Cordinate"<< endl;
    cin >> x;
    cout << "Please Enter y-Cordinate" << endl;
    cin >> y;

    Point point(x,y);

    //just putting here in main to show that X and Y value isn't passed
    point.someMethods(); <-- the output will always be X:0 Y:0 no matter what value the user input
}

【讨论】:

  • 感谢您的帮助。设法理解它并在你们的帮助下得到它
【解决方案3】:

问题

你没有修改你的零初始化变量

Point point; // <---- only this variable is zero-initialized
int main()
{     
    // ...

    //just putting here in main to show that X and Y value isn't passed
    point.someMethods(); //<-- the output will always be X:0 Y:0 no matter what value the user input
//  ^^^^^ <--- again, still zero
}   

解决方案

改为:

int main()
{     
    Point point(1,2); // <-- or read from std::cin

    //just putting here in main to show that X and Y value isn't passed
    point.someMethods(); //<-- value the user input
} 

Live Example

注意

仅仅因为这里有,编写构造函数的规范方法是

class Point
{
private:
   int x = 0; // any constructor which does explicitly set x or y
   int y = 0; // will take these in-class initializer values

public:
   // default constructor uses in-class initializer
   Point() = default; // sets to (0,0)

   // other constructor does NOT use setters but initialization list
   Point(int a, int b): x{a}, y{b} {} // sets to (a,b)
};

【讨论】:

  • 感谢您的帮助。设法理解它并在你们的帮助下得到它
  • @jeremykeh 请注意,您以非常不习惯的方式编写了构造函数,请参阅我的答案中的注释以了解如何在 C++11 中执行此操作
【解决方案4】:

在主函数中,你有语句

Point Point(x,y);

当我认为你的意思时

Point point(x,y);

事实上,下面的 point.someMethods() 调用使用了在 main 函数之前声明的全局 point 对象,我认为这是问题所在。

【讨论】:

  • 感谢您的帮助。设法理解它并在你们的帮助下得到它
【解决方案5】:

您正在调用全局点对象上的 someMethods(),该点对象是使用将 x 和 y 值初始化为 0 的默认构造函数创建的。

使用下面的代码。

#include <iostream> 
#include "Point.h"
using namespace std;
int main()
{     
    int x,y;
    cout << "Please Enter x-Cordinate"<< endl;
    cin >> x;
    cout << "Please Enter y-Cordinate" << endl;
    cin >> y;

    Point point(x,y);

    //just putting here in main to show that X and Y value isn't passed
    point.someMethods(); // This should print the proper X and Y values
}

【讨论】:

  • 感谢您的帮助。设法理解它并在你们的帮助下得到它
【解决方案6】:

你需要改变Point Point(x,y);到Point point(x,y)并删除Point point;

【讨论】:

  • 感谢您的帮助。设法理解它并在你们的帮助下得到它
猜你喜欢
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 2021-04-14
相关资源
最近更新 更多