我试图在用户输入后得到x和y的值,
将值放在一个consturctor中,
并使用getX()
中的getY()
和Point.cpp
方法进行一些计算,但问题是,它始终返回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;
}
Point.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
答案 0 :(得分:3)
在main函数中,您有语句
Point Point(x,y);
我认为你的意思
Point point(x,y);
实际上,下面的point.someMethods()
调用是使用在main函数之前声明的全局point
对象,我认为这是问题所在。
答案 1 :(得分:3)
您正在使用默认构造函数创建的全局点对象上调用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
}
答案 2 :(得分:2)
您需要注意自己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函数。)
答案 3 :(得分:2)
这是因为您的程序中有2个“Point”对象。一个在这里实例化:“点数点”;另一个在这里实例化:“Point Point(x,y);”。
最后你要调用“point.someMethods();”使用第一个对象,它是使用默认构造函数构造的,因此将x和y设置为0。
我相信在这种情况下你应该删除“点数点”;从全局命名空间实例化,并更改“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
}
答案 4 :(得分:2)
您没有修改零初始化变量
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
}
仅仅因为你在这里有c++11,编写构造函数的规范方法就是
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)
};
答案 5 :(得分:2)
你需要改变Point Point(x,y);到点(x,y)并删除点数;