【问题标题】:C++ function argument and struct errorsC++ 函数参数和结构错误
【发布时间】:2016-02-06 00:50:21
【问题描述】:

我目前正在编写一个程序,该程序将允许用户输入方向并沿坐标平面移动机器人。我是 C++ 新手,所以我遇到的一些错误让我感到困惑。有人可以解释我的错误吗?

当我调用函数 init_room 时,它说参数太少。当我调用 init_robot 时也会发生同样的事情。它们都有指针参数。我该如何解决这个问题?

当我在主函数中调用函数 move 时,它​​表示表达式必须是可修改的值。这是什么意思?

感谢您的帮助!

    #include <iostream>

using namespace std;

typedef struct room {
    int x;
    int y;
};

typedef struct robot {
    room *current_room;
    int current_x;
    int current_y;
    int model;
};

void init_robot(robot *r) {
    r->current_room->x;
    r->current_room->y;
    r->current_x = 0;
    r->current_y = 0;
    //Assign model number
    r->model;
}

void init_room(room *r) {
    cin >> r->x;
    cin >> r->y;

}

char move(robot *r, int direction) {
        if (direction == 'N' || direction == 'n')
        {
            r->current_y + 1;
        }
        else if (direction == 'S' || direction == 's')
        {
            r->current_y - 1;
        }
        else if (direction == 'E' || direction == 'e')
        {
            r->current_x + 1;
        }
        else if (direction == 'W' || direction == 'w')
        {
            r->current_x - 1;
        }
        else
        {
            direction = 'x';
            return direction;
        }

    int main() {
        char direction;
        char restart;

        cout << "What size would you like the room to be?";

        room rr;

        init_room();

        robot r; initrobot();

        while (true) {

                cout << "Your robot is in a room with the dimensions (" << rr.x << "," << rr.yy << "). at location (" << r.current_x << "," << r.current_y << ")." << endl;
                cout << "What direction would you like to move? N (North), E (east), S(South), or W(West)?";
                do {
                cin >> direction;

                move(direction) = direction;
                if (direction = 'x') {
                    cout << "Invalid direction. Please enter N, E, S, or W.";
                    }
                while (direction == 'x');

                cout << "Current position is" << endl;

    //          if (r.current_x <= rr.x && r.current_y <= rr.y)
        //      {
            //      cout << "Error, your robot is out of the room bounds. Your robot has exited the room. Would you like to enter another room? Y or N?";
                    cin >> restart;
            //} while (restart == 'Y' || restart == 'y');
                    }

            system("pause");
            return 0;
        }

【问题讨论】:

    标签: c++ function struct parameters arguments


    【解决方案1】:

    函数 init_room 和 init_robot 都采用一个参数。所以,你给他们的电话应该是这样的

    init_room(&rr);
    init_robot(&r);
    

    move 返回一个char 类型的右值,并且右值不能被修改。它只能用作其他表达式的输入。我希望你不知道什么是右值,你最好研究一下它,因为它是 C++ 中的一个重要概念。 (快速版本:右值可能位于 = 的右侧,基本上是只读的。)

    【讨论】:

    • 感谢两位的帮助! @GaryMakin 我一定会研究一下右值是什么。感谢您的提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    相关资源
    最近更新 更多