C++ 无法调用getter方法
我试图在用户输入后获得x和y的值, 将值放入constructor中, 使用C++ 无法调用getter方法,c++,visual-c++,c++11,c++-cli,c++builder,C++,Visual C++,C++11,C++ Cli,C++builder,我试图在用户输入后获得x和y的值, 将值放入constructor中, 使用Point.cpp中的getX()和getY()方法进行一些计算,但问题是,它总是返回X:0y:0,我不知道为什么。我的代码在下面 Point.cpp #include <iostream> #include "Point.h" using namespace std; Point::Point(int x,int y) { setX(x); setY(y);
Point.cpp
中的getX()
和getY()
方法进行一些计算,但问题是,它总是返回X:0y: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;
}
您需要注意的是,您确实使用了
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
}
intx,y;
int main()
{
cout x;
库蒂;
点(x,y);
//只是把这里放在main中,以显示X和Y值没有被传递
point.someMethods();您需要注意,您确实使用了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
}
intx,y;
int main()
{
cout x;
库蒂;
点(x,y);
//只是把这里放在main中,以显示X和Y值没有被传递
之所以发生这种情况,是因为程序中有两个“点”对象。一个在这里实例化:“点点”;另一个在这里实例化:“点(x,y);”
最后,您将使用第一个对象调用“point.someMethods();”,该对象是使用默认构造函数构造的,因此将x和y设置为0
我认为在这种情况下,您应该从全局命名空间中删除“Point;”实例化,并将“Point(x,y);”的名称更改为“Point(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
}
#包括
#包括“h点”
使用名称空间std;
int x,y;
int main()
{
cout x;
库蒂;
点(x,y);
//只是把这里放在main中,以显示X和Y值没有被传递
之所以发生这种情况,是因为程序中有两个“点”对象。一个在这里实例化:“点点”;另一个在这里实例化:“点(x,y);”
最后,您将使用第一个对象调用“point.someMethods();”,该对象是使用默认构造函数构造的,因此将x和y设置为0
我认为在这种情况下,您应该从全局命名空间中删除“Point;”实例化,并将“Point(x,y);”的名称更改为“Point(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
}
#包括
#包括“h点”
使用名称空间std;
int x,y;
int main()
{
cout x;
库蒂;
点(x,y);
//只是把这里放在main中,以显示X和Y值没有被传递
问题
您没有修改零初始化变量
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
}
问题
您没有修改零初始化变量
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
}
在main函数中,有以下语句
Point Point(x,y);
当我认为你的意思是
Point point(x,y);
实际上,下面的point.someMethods()
调用使用的是在main函数前面声明的全局point
对象,我认为这就是问题所在。在main函数中,您有以下语句
Point Point(x,y);
当我认为你的意思是
Point point(x,y);
实际上,下面的point.someMethods()
调用正在使用在main函数之前声明的全局point
对象,我认为这就是问题所在。您正在调用point对象上的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
}
#包括
#包括“h点”
使用名称空间std;
int main()
{
int x,y;
cout x;
库蒂;
点(x,y);
//只是把这里放在main中,以显示X和Y值没有被传递
point.someMethods();//这应该打印正确的X和Y值
}
您正在调用点对象上的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
}
#包括
#包括“h点”
使用名称空间std;
int main()
{
int x,y;
cout x;
库蒂;
点(x,y);
//只是把这里放在main中,以显示X和Y值没有被传递
point.someMethods();//这应该打印正确的X和Y值
}
您需要更改点(x,y);到点(x,y)并删除点;您需要更改点(x,y);到点(x,y)并删除点;将点(x,y);
替换为点.setX(x);
和点.setY(y);
(在当前示例中,您从未实际修改过点
)。感谢您的帮助。请设法理解它,并在您的帮助下获得它替换点
点
使用点.setX(x);
和点.setY(y);
(在当前示例中,您从未实际修改点
).谢谢你的帮助。设法理解它并从你的帮助中获得它谢谢你的帮助。设法理解它并从你的帮助中获得它guys@jeremykeh请注意,您以一种非常非惯用的方式编写了构造函数,请参阅我的答案中的注释,了解如何在C++11中执行此操作。谢谢您的帮助。请设法理解它在你的帮助下就可以得到它guys@jeremykeh请注意,您已经以一种非常非惯用的方式编写了构造函数,请参阅我的答案中的注释,了解如何在C++11中执行此操作。感谢您的帮助。请设法理解它,并从您的帮助中获得它。感谢您的帮助。请设法理解它,并从您的帮助中获得它男人感谢帮助。设法理解它并从你男人的帮助中得到它。设法理解它并从你男人的帮助中得到它。设法理解它并从你男人的帮助中得到它。设法理解它并从你男人的帮助中得到它。设法理解它并从你男人的帮助中得到它谢谢你的帮助。设法理解它,并从你的帮助中得到它。设法理解它,并从你的帮助中得到它