无法使用成员函数更改私有变量

Unable to change private variables using member functions

本文关键字:变量 函数 成员      更新时间:2023-10-16
#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

即,函数内部的p1mainp1的副本,一旦函数返回就会被删除。如果要更改在函数中作为参数传递的点,则通过引用传递它:

int rotate(int x, int y, Point& p1);
                            ^^--------pass-by-reference

PS:...但是,由于rotatePoint的成员函数,因此您可能应该轮换调用它的实例,将其签名更改为

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;
}

是基本旋转所需的一切