【发布时间】: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