首页 > 解决方案 > 变量 z 不能从不同的函数访问变量 x

问题描述

查看我提供的代码,我正在尝试使其 z = x

但它不起作用,因为 x 在不同的函数中。一直说 x 没有声明。有没有什么办法解决这一问题?

class game{

    public:
        void Starter(){
                string z {"Test"};
                z = x;
        }

    private:
        void Questions(){
                string x;
                cout << "Welcome to this game, please answer with yes or no: \n \n";

标签: c++

解决方案


你需要传递xz.

void Starter(string x){
    string z {"Test"};
    z = x;
}

然后在你的其他功能中你可以做Starter(x);


推荐阅读