gpt4 book ai didi

c++ - 无法在两个函数之间复制值

转载 作者:行者123 更新时间:2023-11-28 02:29:56 27 4
gpt4 key购买 nike

这是我的代码:

void Draw() {
int x = 57;
int y = 500;
int temp = x;
int colour;
for (int i = 0; i <= 13; ++i){
for (int j = 0; j <= 9; ++j){
if (i % 2 == 0){
colour = 3;
}
else colour = 4;
DrawRectangle(x, y, 67, 30, colors[colour]);
x = x + 67;
}
y = y - 30;
x = temp;
}


DrawCircle(100, 100, 10, colors[2]);
DrawRectangle(20, 0, 95, 12, colors[1]);


}

void Move(int key, int x, int y) {
if (key == GLUT_KEY_LEFT) { // left arrow key is pressed

}
else if (key == GLUT_KEY_RIGHT) { // right arrow key is pressed


}
glutPostRedisplay(); // Redo- the drawing by calling
}

这是我在一个类中的两个函数。我需要将 x 和 y 的值从 Move() 复制到 Draw(),但 Draw() 不接受任何参数,还有什么其他方法可以做到这一点。此外,如果有人需要完整代码,他可以索取。

最佳答案

选项1

您可以将函数签名更改为 Draw(int x, int y)。虽然您没有声明您不能更改函数签名,但我猜这个选项是不可能的。

选项 2

您说这些是类的成员函数。因此,您需要将变量的范围扩大到 Move 函数之外。您可以通过使它们成为成员变量来实现这一点。例如:

class Foo
{
public:
Foo() :
mX(0),
mY(0)
{
// Do nothing
}

void Draw()
{
... code in here that uses mX and mY ...
}

void Move(int key, int x, int y)
{
mX = x;
mY = y;

... other code ...
}

private:
// Class member variables accessible by all functions in the class
int mX;
int mY;
};

关于c++ - 无法在两个函数之间复制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29313878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com