无法使用成员函数更改私有变量
Unable to change private variables using member functions
#include <iostream>
#include <string>
using namespace std;
struct Point {
private:
int xCord,yCord;
public:
void setX(int x);
void setY(int y);
int getX();
int getY();
int rotate(int x, int y, Point p1);
int moveHorizontally(int x, int a, int b);
int moveVertically(int y, int a, int b);
};
int main() {
Point p1;
p1.setX(1); //sets X
p1.setY(2); //sets Y
cout << p1.getX() << ", " << p1.getY() << endl; //prints current value of X & Y
p1.rotate(p1.getX(), p1.getY(), p1);
cout << p1.getX() << ", " << p1.getY() << endl;
return 0;
}
void Point::setX(int newX) {
xCord = newX;
}
void Point::setY(int newY) {
yCord = newY;
}
int Point::getY() { //This will just return the y Cord.
return yCord;
}
int Point::getX() { //This will just return the x Cord.
return xCord;
}
int Point::moveHorizontally(int x, int tempX, int tempY) {
//Move the point to the right if positive.
//Move the point to the left if negative.
int newX = tempX + (x);
return newX;
}
int Point::moveVertically(int y, int tempX, int tempY) {
//Move the point up if positive.
//Move the point down if negative.
int newY = tempY + (y);
return newY;
}
int Point::rotate(int tempX, int tempY, Point p1){
//(1,2) -->> (-2,1)
int tempX_DNC = tempX;
int tempY_DNC = tempY;
int quadrant;
if((tempX > 0) && (tempY > 0)) { //Quadrant 1: x(positive), y(positive) Then rotates to Quad 2
quadrant = 1;
tempX = -(tempY);
tempY = tempX_DNC;
} else if ((tempX < 0) && (tempY > 0)) { //Quadrant 2: x(negative), y(positive) Then rotates to Quad 3
quadrant = 2;
tempX = -(tempY_DNC);
tempY = tempX_DNC;
} else if ((tempX < 0) && (tempY < 0)) { //Quadrant 3: x(negative), y(negative) Then rotates to Quad 4
quadrant = 3;
tempX = -(tempY_DNC);
tempY = tempX_DNC;
} else if ((tempX > 0) && (tempY < 0)) { //Quadrant 4: x(positive), y(negative) Then rotates to Quad 1
quadrant = 4;
tempX = -(tempY_DNC);
tempY = tempX_DNC;
} else {
quadrant = 0;
}
//This will rotate the points 90* to the left.
//(1,2) will then become (-2,1)
//I could have if in quadrant1, all are positive, if in quadrant 2 the x would be negative and y would be positive
//If in quadrant 3 the x and y will both be negative, if in quadrant 4 the x would be positive and the y would be negative
cout << tempX << ", " << tempY << endl;
p1.setX(tempX);
p1.setY(tempY);
cout <<"x is: " <<p1.getX() <<endl;
cout <<"Y is: " <<p1.getY() <<endl;
}
代码在上面。
所以我正在创建一个类点。点有 2 个私有变量 xCord,yCord。我想调用旋转函数,并使其能够修改 xCord、yCord,但它没有。我不知道为什么。我尝试将点 p1 传递给函数,看看这是否可以解决问题,但没有,我也尝试不传递点 p1 而只是在函数定义中具有点 p1。
p1.setX(变量(;当它在 main(( 中时工作。但当我在另一个成员函数中调用 p1.setX(变量(时不会。
您正在将p1
的副本传递给rotate
函数。仅修改此副本。
按值传递点:
int rotate(int x, int y, Point p1);
^^--------pass-by-value
即,函数内部的p1
是main
中p1
的副本,一旦函数返回就会被删除。如果要更改在函数中作为参数传递的点,则通过引用传递它:
int rotate(int x, int y, Point& p1);
^^--------pass-by-reference
PS:...但是,由于rotate
是Point
的成员函数,因此您可能应该轮换调用它的实例,将其签名更改为
int rotate(int x, int y);
而不是更改作为参数传递的某个点的坐标,请执行以下操作:
this->setX(tempX); // this-> not really necessary, just added for clarity
this->setY(tempY);
或者,您可以保持原样并将应该旋转的点作为参数传递,但是您应该考虑使该方法static
。
PPS:如果要将其更改为按引用传递,则必须将类声明中的签名更改为:
int rotate(int x, int y, Point& p1);
以及您必须更改为的定义:
int Point::rotate(int tempX, int tempY, Point& p1) { /*...*/ }
void point::rotate() {
xcord = ycord;
ycord = xcord;
}
是基本旋转所需的一切
相关文章:
- 是否可以将变量/函数返回作为模板参数传递
- 是否有一种方法可以修复编译器未找到特定的OPENCV变量/函数
- 访问实例化类的变量/函数
- 如何在变量函数参数列表中检测给定类型的参数的可用性,并在处理完所有参数后采取行动
- C 多组变量函数参数
- 最有cpu效率的方法是使用std::算法和变量函数模板的参数
- 在此作用域中未声明条件变量函数
- C 关于将数组复制到向量并调用其变量 /函数
- 仅在C 中的一个变量上,两个变量函数的数值集成(使用数值配方库)
- 在应用变量函数的矩阵元素上循环
- 如何向调试器隐藏变量/函数名
- C++11变量函数类型模板参数
- 变量函数参数的奇怪问题
- C++变量函数和cout
- c++模板变量函数未定义引用
- 我可以使用枚举类值作为变量函数的参数吗?
- 从另一个类访问变量/函数
- <"变量/函数"的多重定义>
- 从c++到C文件中定义的变量/函数的未定义引用
- 为什么转换变量函数参数很重要?