C++中如何从另一个类获取成员变量

问题描述 投票:0回答:1

我想从另一个类获取

public:
成员变量。 但我无法得到它们。你能指导我怎么做吗?

每个类中都有

hand
变量
User
Computer

我想在

doJudge()
类中的
std::cout
方法的
Judge
处获取它们,这意味着
user.hand
computer.hand

但是这些值表示:

User CHOSE hand in class Judge := 7345888
Computer CHOSE hand in class Judge := 0

这是我的代码:

#include <iostream>
#include <random>
#include <ctime>

using namespace std;

int random(int low, int high)
{
    return low + rand() % (high - low + 1);
}

class User
{
    public :
        User(){};  //constructor

        int hand;
        void setHand()
        {
            std::cout << "What you choose? (ROCK = 1, SCISSORS = 2, PAPER = 3) := ";
            std::cin>> hand;
        }
};

class Computer
{
    public :
        Computer(){}; //constructor

        int hand;
        void setHand()
        {
            hand = random(1, 3);
            std::cout << "Computer choose := " << hand << std::endl;
        }
};

class Judge
{
    public :
        User user;
        Computer computer;
        
        Judge(){}; //constructor

        void doJudge()
        {
            std::cout << "User CHOSE hand in class Judge := " << user.hand << std::endl;
            std::cout << "Computer CHOSE hand in class Judge := " << computer.hand << std::endl;

        }
};

int main()
{
    srand(time(NULL));

    User user;
    user.setHand();

    Computer computer;
    computer.setHand();

    Judge judge;
    judge.doJudge();
}

编译并执行:

C:\RPS>g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

C:\RPS>rps.exe
What you choose? (ROCK = 1, SCISSORS = 2, PAPER = 3) := 2
Computer choose := 2
User CHOSE hand in class Judge := 7345888
Computer CHOSE hand in class Judge := 0

我已经花了一个多星期了,但我要放弃了。

请向我解释为什么我无法获得我和计算机选择的相同值?

c++ class variables member
1个回答
0
投票

user
内部的
computer
main()
变量与
user
类内部的
computer
Judge
成员不同。 您填充前者,然后对后者采取行动。

您应该删除

Judge
内部的变量,并将它们作为参数传递给
doJudge()
,例如:

class Judge
{
    public :
        Judge(){}; //constructor

        void doJudge(const User &user, const Computer &computer)
        {
            std::cout << "User CHOSE hand in class Judge := " << user.hand << std::endl;
            std::cout << "Computer CHOSE hand in class Judge := " << computer.hand << std::endl;
        }
};

int main()
{
    srand(time(NULL));

    User user;
    user.setHand();

    Computer computer;
    computer.setHand();

    Judge judge;
    judge.doJudge(user, computer);
}

或者,你应该让

Judge
内部的变量指向/引用
main()
内部的变量,例如:

class Judge
{
    public :
        User& user;
        Computer& computer;
        
        Judge(User& user, Computer& computer) : user(user), computer(computer) {};

        void doJudge()
        {
            std::cout << "User CHOSE hand in class Judge := " << user.hand << std::endl;
            std::cout << "Computer CHOSE hand in class Judge := " << computer.hand << std::endl;
        }
};

int main()
{
    srand(time(NULL));

    User user;
    user.setHand();

    Computer computer;
    computer.setHand();

    Judge judge(user, computer);
    judge.doJudge();
}
© www.soinside.com 2019 - 2024. All rights reserved.